-->

asp.net MVC 4 Telerik Grid Ajax issue

2019-08-11 00:44发布

问题:

i am facing an issue with mvc telerik grid there is my view :

@(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()
    )

and there is my controller code

[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
            });
        }
    }

on load the data is loaded correctly from database but internal server error 500 occur when i click on paging or sort data.

thanks in advance.

回答1:

you are using a Linq query inside a disposable scope.but the query execution is deferred until using it (when you have left using {} scope). and you'r context is disposed! solution :

add .ToList at the end of query :

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