Asp.net-Mvc

如何在 asp.net MVC 4 Razor 中綁定劍道網格

  • August 3, 2016

我已經創建了 asp.net MVC 4 應用程序,我在其中使用實體框架,並且“數據”類是模型。

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

現在我想將表格的數據顯示到劍道網格。在控制器中,我使用以下程式碼:

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

現在我不知道在 Kendo Grid 中顯示數據(我是 kendo 和 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
)

終於得到答案:

看法:

@(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; }
   }

引用自:https://stackoverflow.com/questions/18465337