8

我已经创建了 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
)
4

2 回答 2

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; }
    }
于 2013-08-27T13:13:37.730 回答
4

如果您的控制器名称是 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"))))
于 2013-08-27T17:08:48.263 回答