【问题标题】:Server Side Pagination not working for the first page kendo grid mvc wrapper服务器端分页不适用于第一页剑道网格 mvc 包装器
【发布时间】:2015-07-10 08:00:22
【问题描述】:

如果数据超过 15000,我的剑道网格需要太多时间来加载 记录,我想实现服务器端分页。

我没有使用实体框架。我正在使用 Kendo MVC 包装器和 SQL 用于拉取和显示数据的服务器存储过程。

我已将两个参数作为@Skip 和@Take 传递给Procedure。

以下是我的代码:

查看

@(Html.Kendo().Grid(Model)
    .Name("expandedView_" + Model.ToArray()[0].WidgetId.ToString())
    .Columns(columns =>
    {
        columns.Bound(p => p.FacilityNameAbbreviation).Title(@Resource.FacilityName).Width("15%");
        columns.Bound(p => p.DeviceAbbreviation).Title(@Resource.DeviceName).Width("10%");
        columns.Bound(p => p.EventType).Title(@Resource.EventType).Width("40%");
        columns.Bound(p => p.SubEventCode).Title(@Resource.SubAlarmCode).Width("10%").HtmlAttributes(new { style = "text-align:right" });
        columns.Bound(p => p.FacilityEventTime).Title(@Resource.EventTime).Width("15%");
        columns.Bound(p => p.EventStatus).Title(@Resource.Status).Width("10%");
        columns.Bound(p => p.PriorityColor).Hidden(true);
    })
    .Scrollable()
    .Pageable()
    .DataSource(dt => dt
    .Ajax()
    .PageSize(100)
    .Read(read => read.Action("PageWiseData", "DeviceEvent", new { WidgetId = Model.ToArray()[0].WidgetId, FacilityIds = ViewData["Facids"].ToString() }))
    .ServerOperation(true)
    .Model(model => model.Id(p => p.FacilityEventTime)))
)

行动:

[HandleError(ExceptionType = typeof(Exception), View = "ApplicationError")]
public PartialViewResult DeviceEventExpandedView(int WidgetId, string FacilityIds)
{
    var userDetail = sessionContext.UserContextBag;

    int Skip = 0; // for first page data static value is passed
    int Take = 100;

    IList<AMI.WebRole.Models.Widgets.DeviceEventModel> result = _deviceEventClient.DeviceEventsForExpandedView(WidgetId, FacilityIds, TenantId, CustomerId, UserId, this.sessionContext.UserContextBag.CultureName, Skip, Take);

    return PartialView(result);
}


public ActionResult PageWiseData(int WidgetId, string FacilityIds, [DataSourceRequest]DataSourceRequest request)
{
    IList<AMI.WebRole.Models.Widgets.DeviceEventModel> Totalresult = null;

    var Skip = (request.Page - 1) * request.PageSize;
    var Take = request.PageSize;

    Totalresult = _deviceEventClient.DeviceEventsForExpandedView(WidgetId, FacilityIds, TenantId, CustomerId, UserId, this.sessionContext.UserContextBag.CultureName, Skip, Take);

    return Json(new
        {
            Data = Totalresult,
            Total = 1500
        });
}

问题:

第一次调用“DeviceEventExpandedView”,加载视图 数据显示成功,但页码显示为 1 只有第一次。在我的网格中,过滤器也在那里,一旦我 单击过滤器再次调用方法“PageWiseData”它拉100 记录,这次是显示后续页面的页码。作为 根据我的理解,第二次返回的数据是 JSON 格式 还包括 Total=1500,但在第一种情况下,当部分视图为 已加载,未设置此计数“Total”。

加载部分视图并且第一个绑定网格时,我无法显示页码(在我的情况下应该最多 15 个) 时间

任何人都可以在这里提供帮助,如何第一次通过计数或如何 第一次使用方法“PageWiseData”绑定网格....或任何 其他方法...请帮助!

【问题讨论】:

  • 您的 Action 做错了,让 Kendo 过滤您的结果,如 @hutchonoid 所述。
  • 我想,为此我必须先从数据库中提取所有记录,对吗??
  • 不,.ToDataSourceResult(request) 将对查询应用自己的过滤器和分页,因此只会检索所需的记录。

标签: c# asp.net asp.net-mvc kendo-grid kendo-asp.net-mvc


【解决方案1】:

如果您应用以下内容,则无需关心或担心自己处理分页。

  • 将您的 totalResult 变量改为 IQueryable&lt;AMI.WebRole.Models.Widgets.DeviceEventModel&gt;
  • 更改DeviceEventsForExpandedView 方法,删除SkipTake 参数并确保它返回IQueryable&lt;AMI.WebRole.Models.Widgets.DeviceEventModel&gt;

如果你返回IQueryable Kendo 应该在数据库而不是内存中应用排序和过滤。

还要确保您使用.ToDataSourceResult(request) 来返回结果,如下所示:

public ActionResult PageWiseData(int WidgetId, string FacilityIds, [DataSourceRequest]DataSourceRequest request)
{
    IQueryable<AMI.WebRole.Models.Widgets.DeviceEventModel> totalresult = null;

    var totalresults = _deviceEventClient.DeviceEventsForExpandedView(WidgetId, 
          FacilityIds, TenantId, CustomerId, UserId,
           this.sessionContext.UserContextBag.CultureName);

        var results = totalresults.ToDataSourceResult(request);
        return Json(results);

}

关于这个主题的好文章:

Server Paging, Sorting, and Filtering with Kendo DataSourceRequest

【讨论】:

  • 我想,为此我必须先从数据库中提取所有记录,对吗??
  • @devgal 是的,只要确保它返回一个延迟的IQueryable。 :)
  • 但是如果我提取所有记录,我的存储过程需要 42 秒,如果我通过跳过并执行 100 条记录的过程,则需要 13 秒....我不想从中提取所有记录DB本身....有可能吗??
  • 它不会拉取所有记录。 Kendo 会将其过滤器和分页应用于查询,然后它将拉出 relevant 记录。
  • @devgal Kendo 只会调用您在标记中指定的方法。
猜你喜欢
  • 2013-07-08
  • 1970-01-01
  • 2023-03-16
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多