【问题标题】:KendoUI MVC grid doesn't show data for pages except first pageKendoUI MVC 网格不显示除第一页以外的页面数据
【发布时间】:2019-06-27 02:27:52
【问题描述】:

我有一个 KendoUI MVC 网格如下:

@(Html.Kendo().Grid<MonitoringVm>()
          .Name("monitoringGrid")
          .Columns(columns =>
          {
              columns.Bound(c => c.RecordDateTime).Title("Date Time").Format("{0:dd/MM/yyyy hh:mmtt}").Width(149);
              columns.Bound(c => c.SourceEndpointName).Title("Source").Width(100).Sortable(true);
              columns.Bound(c => c.DestinationEndpointName).Title("Destination").Width(100);
              columns.Bound(c => c.TagName).Title("Tag").Width(120);
              columns.Bound(c => c.ErrorDescription).Title("Description");
              columns.Bound(c => c.LogTransferErrorId).Title("Retry")
                  .ClientTemplate("<div style='text-align: center;'><button class='k-button k-primary' type='button' onclick=\"javascript: retryRequest(this);\">Retry</button></div>")
                  .HeaderTemplate("<div style='text-align: center;'>Retry</div>")
                  .Width(50);
              columns.Bound(c => c.LogTransferErrorId).Title("Delete")
                  .ClientTemplate("<div style='text-align: center;'><img src='/Content/Images/iconfinder_bin.png' onclick=\"javascript: confirmArchiveError('#:data.LogTransferErrorId#');\" class='archive-error'/></div>")
                  .HeaderTemplate("<div style='text-align: center;'>Delete</div>")
                  .Width(50);
          }
          )
          .Pageable()
          .Sortable(sortable => sortable.AllowUnsort(true))
          .ClientDetailTemplateId("template")
          .DataSource(dataSource => dataSource
              .Ajax()
              .PageSize(25)
              .ServerOperation(true)
              .Read(read => read.Action("ReadErrorRecords", "Monitoring"))
          )
          .Events(events => events.DataBound("dataBound")))

对应的控制器如下:

private readonly JsonHttpClient _client;
public JsonResult ReadErrorRecords([DataSourceRequest]DataSourceRequest request)
    {
        var urlEndpoint = $"Monitor/ErrorRecords?page={request.Page}&pageSize={request.PageSize}";
        var response = _client.Get<MonitoringVmResponse>(urlEndpoint);

        DataSourceResult result = response.Results.ToDataSourceResult(request);
        result.Total = response.Count ?? 0;
        return Json(result);
    }

当我导航到不同的页面时,会调用上面的控制器方法。在调试器中,我观察到我收到了正确的数据,并且从控制器返回了相同的数据。

在 UI 上,对于第一页之后的后续页面,不显示数据。 我错过了什么?

【问题讨论】:

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


    【解决方案1】:

    您似乎要进行两次分页。

    假设您发出了第二页的请求。 我想您的 _client.Get&lt;MonitoringVmResponse&gt;(urlEndpoint) 会返回从 26 到 50 的记录。 但稍后您调用response.Results.ToDataSourceResult(request),这也适用于内部.Skip(25).Take(25),因此您之前调用的所有项目都将被跳过,并且不会将任何数据返回到您的视图中。

    我想有两种可能的解决方法:

    1. 除了.ToDataSourceResult(request),还可以使用其他剑道扩展方法,例如GroupByOrderByWhere 等。
    2. 可以通过提供值 1 作为request.Page 来“欺骗”.ToDataSourceResult(request) 方法:
        var response = _client.Get<MonitoringVmResponse>(urlEndpoint);
        request.Page = 1;
        DataSourceResult result = response.Results.ToDataSourceResult(request);
    

    【讨论】:

    • 是的,第二种解决方法效果很好!第三种解决方法是根本不使用ToDataSourceResult(request) 方法。创建您自己的DataSourceResult 对象,然后编写您自己的排序逻辑,例如this
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2021-01-18
    相关资源
    最近更新 更多