【问题标题】:How to bind the Kendo Grid in asp.net MVC 4 Razor如何在 asp.net MVC 4 Razor 中绑定剑道网格
【发布时间】:2013-08-30 04:40:27
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    终于得到答案:

    查看:

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

    【讨论】:

    • 如果 jquery 初始化也是这样呢?
    【解决方案2】:

    如果您的控制器名称是数据,那么您可以使用以下内容

    你的模型

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

    【讨论】:

    • .Read(read =&gt; read.Action("Index", "Data")))) 我猜应该是index 而不是ReadDegrees
    • 我在Html.KendoGrid 遇到错误 Htmlhelper 不包含“Kendo”的定义..但我在 View 和 Project 的 web.config 中添加了 Kendo.MVC.dll 的引用。你知道如何消除错误吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多