【问题标题】:Set Kendo UI Grid Page Index in the MVC Controller在 MVC 控制器中设置 Kendo UI 网格页面索引
【发布时间】:2013-12-22 21:38:55
【问题描述】:

我正在尝试在控制器上为剑道网格设置页面索引,以避免客户端分页。使用客户端分页时,除了第一页之外不显示任何记录。从控制器调用数据时,我只返回页面所需的 10 条记录。数据调用包括 Skip() 和 Take() 函数,仅从服务器返回所需,而不是加载整个网格。

.cshtml

@(Html.Kendo().Grid<Reckon.Service.Payroll.Data.DTO.EmployeeDto>()
        .Name("EmployeeGrid")
        .Columns(cols => 
        {
                cols.Bound(emp => emp.Id).Title("ID").Hidden();
                cols.Bound(emp => emp.EmployeeNumber).Title("Employee ID").Width(100);
                cols.Bound(emp => emp.IsPayRunReady).Title("Status").Width(10).ClientTemplate("<span title='This employee is #= IsPayRunReady ? '': 'not '#payrun ready.' class='#= IsPayRunReady ? 'okICN-small' : 'alertICN-small'#'>#= IsPayRunReady ? '': 'Not' # #= IsPayRunReady ? 'P':'p'#ayrun ready</span>");
                cols.Bound(emp => emp.FirstName).Title("First Name").Width(100);
                cols.Bound(emp => emp.LastName).Title("Last Name").Width(100);
                cols.Bound(emp => emp.DateOfBirth).Title("DOB").Format("{0:dd/MM/yyyy}").Width(100);
                cols.Template(@<text></text>).ClientTemplate("<a href='" + Url.Action("EmployeeDetailEdit", "EmployeeDetail") + "/#=Id#'>Edit</a>").Width(50);
                cols.Template(@<text></text>).ClientTemplate("<a href='" + Url.Action("EmployeeDetailRead", "EmployeeDetailRead") + "/#=Id#'>View</a>").Width(50);
                cols.Template(@<text></text>).ClientTemplate("<a class='k-button xxx' tag='#=Id#'>Delete</a>").Width(50);
        })
            .Pageable(pageable => pageable.ButtonCount(5))
            .Sortable(sortable => sortable.AllowUnsort(false))
            .Filterable()
            .Resizable(resize => resize.Columns(true))
            .Reorderable(reorder => reorder.Columns(true))
            .Navigatable()
            .Events(evt => evt.DataBound("afterGridLoaded"))
            .DataSource(dataSource => dataSource
                                    .Ajax()
                                    .Batch(true)
                                    .PageSize(10)
                                    .ServerOperation(false)
                                    .Model(model =>
                                    {
                                        model.Id(emp => emp.Id);
                                    })
                                    .Read(read => read.Action("EmployeeListPerPage", "EmployeeDetail"))
                                   )
      )

.cs

public ActionResult EmployeeListPerPage([DataSourceRequest] DataSourceRequest request)
    {
        Dispose();
        EmployeeListRequest empList = new EmployeeListRequest();
        empList.PageNum = request.Page;
        empList.PageSize = request.PageSize;
        //empList.OrderBy = null; //request.Sorts.Any() ? "EmployeeNumber" : request.Sorts[0].Member;

        var dataSource = _payrollService.GetEmployeeListPerPage(empList);
        var model = new EmployeeListModel(dataSource);

        DataSourceResult result = model.Employees.ToDataSourceResult(request);
        result.Total = dataSource.Total;            

        // Set the Page index here

        return Json(result, JsonRequestBehavior.AllowGet);            
    }

使用客户端分页时,将返回数据设置为第一页,然后执行客户端分页,不返回结果。

有可能吗? 任何帮助将不胜感激。

【问题讨论】:

  • 你为什么不想使用剑道的分页?
  • 剑道分页是客户端,当数据从服务器端返回时,它只会返回当前页面所需的 10 条记录。这些记录作为前 10 条记录绑定到网格,当您单击第一页以外的任何其他页面时,它不会显示任何其他记录。这就是我不想使用剑道分页的原因,因为我没有将所有数据返回到网格,而只返回当前所选页面所需的数据。
  • 我不明白你为什么要这样做:empList.PageNum = request.Page; 你在那里做了很多额外的工作,基本上是在与剑道自动为你做的事情作斗争。如果您访问一个页面(通过查询字符串),它将为您返回正确的页面。
  • 这个,empList.PageNum = request.Page;,因为函数 GetEmployeeListPerPage 接受了一个 EmployeeListRequest仅参数。可以举个例子吗?
  • 您的GetEmployeeListPerPage() 方法是否返回IQueryable&lt;T&gt;,您使用的是实体框架还是nHibernate?看起来你仍然在尝试与剑道战斗。请参阅此处的第 7 步:docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/…

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


【解决方案1】:

由于您使用的是实体框架,请将您的控制器更改为此,以让 Kendo/EF 负责分页(它将在服务器端):

public ActionResult EmployeeList([DataSourceRequest] DataSourceRequest request)
{
    var employees = from emp in dbContext.Employees
                    select emp;

    return Json(employees.ToDataSourceResult(request), JsonRequestBehavior.
}

这是一个粗略的例子,但应该是一个起点。

【讨论】:

  • 嗨@mmillican,我之前尝试过上述方法,但这会返回完整的数据集,并且我正在使用的当前表有超过 100 万条记录。允许服务器为每个服务器页面或什至在开始时提取所有这些记录将导致服务器上的大量负载。这就是为什么我一次只加载当前页面的详细信息。
  • @pchannon - 看来你的 EF 有什么不正确的地方。它应该推迟执行并且只拉回该页面所需的结果。
猜你喜欢
  • 2014-03-18
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 2012-12-08
  • 2012-10-30
相关资源
最近更新 更多