如何在asp.net MVC 4 Razor中绑定Kendo Grid

时间:2013-08-27 12:23:33

标签: asp.net-mvc asp.net-mvc-4 kendo-ui kendo-grid kendo-asp.net-mvc

我已经创建了asp.net MVC 4应用程序,我正在使用实体框架,类“Data”就是模型。

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

现在我想将表的数据显示到kendo网格。在控制器中,我使用以下代码:

 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
)

2 个答案:

答案 0 :(得分: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; }
    }

答案 1 :(得分:5)

如果你的控制器名称是Data,那么你可以使用以下

你的模特

 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"))))