1

我正面临 mvc telerik grid 的问题,我的观点是:

@(Html.Telerik().Grid(Model.EmployeeList)
.Name("EmployeeGrid")
.Columns(colums =>
    {
        colums.Bound(e => e.First_Name);
        colums.Bound(e => e.Last_Name);
        colums.Bound(e => e.Hire_Date);
        colums.Bound(e => e.Home_Phone);
    })
    .DataBinding(dataBinding => dataBinding.Ajax().Select("_AjaxBinding", "Home"))
    .Groupable()
    .Sortable()
    .Pageable(paging=>paging.PageSize(10))
    .Filterable()
    )

还有我的控制器代码

[AcceptVerbs(HttpVerbs.Post)]
    [GridAction]
    public ActionResult _AjaxBinding(GridCommand command)
    {
        using (var contax=new NorthwindEntities()){
            int pagesize=command.PageSize;
            int page=command.Page;

            var EmployeeList = (from items in contax.Employees
                                select new
                                {
                                    items.First_Name,
                                    items.Last_Name,
                                    items.Hire_Date,
                                    items.Home_Phone
                                });
            return View(new GridModel
            {
                Data = EmployeeList
            });
        }
    }

加载时数据已从数据库正确加载,但当我单击分页或排序数据时发生内部服务器错误 500。

提前致谢。

4

1 回答 1

4

您在一次性范围内使用 Linq 查询。但是查询执行被推迟到使用它(当您离开using {}范围时)。并且您的上下文已处理!解决方案 :

.ToList在查询末尾添加:

var EmployeeList = (from items in contax.Employees
                                select new
                                {
                                    items.First_Name,
                                    items.Last_Name,
                                    items.Hire_Date,
                                    items.Home_Phone
                                }).ToList();
于 2012-12-20T09:22:44.263 回答