8

我已經創建了asp.net MVC 4應用程序,其中我使用的是實體框架和類「數據」是模型。如何在asp.net MVC 4中綁定Kendo網格Razor

AdventureWorksTrainingEntities _dbContext = new AdventureWorksTrainingEntities(); 
Data _data = new Data(); //Model 

現在我想顯示錶的數據到劍道網格。在控制器中,我使用下面的代碼:

public ActionResult Index() 
     { 
      List<Movie> dataForGrid= _dbContext.Movies.ToList(); 
      return View(dataForGrid); 
     } 

現在,我不知道在劍道網格中顯示的數據(我新的劍道和MVC)。我也曾嘗試以下但不工作:

@model IEnumerable<MvcApp.Models.Data> 
@using Kendo.Mvc.UI 
@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Grid For Data</h2> 
Html.Kendo().Grid<Order>() 
    .Name("Grid") 
    .DataSource(dataSource => dataSource // Not implemented 
) 

回答

12

終於拿到答案:

查看:

@(Html.Kendo().Grid<KendoUI.Models.EmployeeViewModel>() 
      .Name("Grid") 
      .Columns(columns => 
      { 
       columns.Bound(p => p.name).Title("Name"); 
       columns.Bound(p => p.gender).Title("Gender"); 
       columns.Bound(p => p.designation).Title("Designation").Width("300px"); 
       columns.Bound(p => p.department).Title("Department").Width("300px"); 
      }) 

      .Editable(editable => editable.Mode(GridEditMode.InLine)) 
      .Navigatable() 
      .Pageable() 
      .Sortable() 
      .Scrollable() 
      .DataSource(dataSource => dataSource // Configure the grid data source 
      .Ajax() 
      .Model(model => 
      { 
       model.Id(x => x.id); 
      }) 
       .Read(read => read.Action("Employee_Read", "Home")) // Set the action method which will return the data in JSON format 
      ) 
      ) 

控制器:

public ActionResult Employee_Read([DataSourceRequest]DataSourceRequest request) 
     { 
      IQueryable<Bhupendra_employees> Details = _dbContext.Bhupendra_employees; 
      DataSourceResult result = Details.ToDataSourceResult(request, p => new EmployeeViewModel 
        { 
         id = p.id, 
         name = p.name, 
         gender = p.gender, 
         designation = p.designation, 
         department = p.Bhupendra_Dept.Dept_Description 
        }); 
      return Json(result, JsonRequestBehavior.AllowGet); 
     } 

型號:

public class EmployeeViewModel 
    { 
     public Int32 id { get; set; } 
     public String name { get; set; } 
     public String gender { get; set; } 
     public String designation { get; set; } 
     public String department { get; set; } 
     //public DateTime dob { get; set; } 
    } 
+0

如果這同樣是在jQuery的初始化呢? – Samra

4

,如果你的控制器名稱的數據,那麼你可以使用下面的

模型

public ActionResult ReadDegrees([DataSourceRequest] DataSourceRequest request) 
    { 
     ViewBag.Countries = CommonController.CountryList(); 
     ViewBag.DegreeTypes = CommonController.DegreeTypeList(); 
     using (var _dbContext= new AdventureWorksTrainingEntities()) 
     { 
      return Json(_dbContext.Movies.ToList.ToDataSourceResult(request)); 
     } 
    } 

你的看法 只要你需要添加下面的假設模型有一個名爲ID

關鍵
Html.Kendo().Grid<Order>() 
.Name("Grid") 
.DataSource(dataSource => dataSource 
    .Ajax() 
    .PageSize(20) 
    .Model(model => model.Id(p => p.ID)) 
    .Read(read => read.Action("ReadDegrees", "Data")))) 
+0

'.Read(read => read.Action(「Index」,「Data」))))''我猜而不是'index'它應該是'ReadDegrees'? – Steve

+0

是的你是對的 – Monah

+0

我在'Html.KendoGrid'上出現錯誤Htmlhelper不包含'Kendo'的定義..但是我在View的web.config中添加了'Kendo.MVC.dll'的引用,以及項目也。你知道如何消除錯誤。 – Steve