【问题标题】:Kendo Server Side Paging Posting to MVC ControllerKendo 服务器端分页发布到 MVC 控制器
【发布时间】:2014-08-19 20:29:37
【问题描述】:

我正在尝试通过 Kendo UI Grid 使用服务器端分页,但我遇到了似乎没有在任何地方记录的错误。

我收到的错误是:

Unhandled exception at line 11, column 15140 in kendo.all.min.js
0x800a138f JavaScript runtime error: Unable to get property 'slice' of undefined or null reference.

根据我在网上找到的文档和其他示例,我使用以下方法绑定我的剑道网格:

var postData = { "searchTerm" : searchTerm, "targetDB" : targetDB, "searchObj" : searchObj }

var grid = $("#grid1").kendoGrid({
    pageable: {
        pageSize: 40
    },
    dataSource: {
        error: function (e) {
            if (e.errors !== false) {
                $("#errorContainer").html(e.errors);
            }
        },
        transport: {
            read: {
                url: servicePath,
                dataType: "json",
                data: postData,
                type: "POST"
            }
        },
        serverPaging: true,
        schema: {
            total: "total",
            data: "data"
        }
    },
    dataBound: function (e) {
        var end = new Date().getTime();
        var time = end - start;
        console.log("Time taken: " + time);
        $("#loading").hide();
        $("#grid1").show();
        $("#totalTimeBadge").html(time + " milliseconds");
        $("#totalCountBadge").html(this.dataSource.total() + " items");
        $("#propertyCountBadge").html($(".k-header").length + " properties");
        logTimes(time, this.dataSource.total(), $(".k-header").length);
    }
});

我使用帖子的原因是(您可能会猜到)我正在实现搜索功能,并使用 Kendo Grid 发布搜索条件和我想要搜索的目标数据库(有两种可能性) .

我已经通过 jslint 运行返回的 JSON 并验证它是有效的 JSON,其结构是:

[{
    "total": 50053,
    "data": [{objects}]
}]

如果我需要发布更大的 JSON 实际摘录,我可以在这里发布。

我在这里跟踪这个问题的答案:Error while implementing basic paging in Kendo UI Grid in ASP.NET

但是这个问题不使用剑道网格来发布和检索项目(不知道这是否会有所不同),而且我还验证了这个问题的唯一答案不适用于我的实例。

如果我取出 total 数据字段并且只返回我的响应的原始数据,网格绑定就好了,但我需要合并服务器端分页。

有什么想法吗?谢谢!

更新

根据下面的建议,我尝试将控制器从返回直接字符串更改为使用 DataSourceResult:

public ActionResult DataSourceResult()
    {
        string dataStr = "[{\"NAME\": \"0720-FI-1081\", \"DESCRIPTION\": \"HP FLARE HEADER DISTRIBUTION\"},{\"NAME\": \"0720-FIC-1002\",\"DESCRIPTION\": null}]";
        DataSourceResult result = new DataSourceResult();
        result.Data = dataStr;
        result.Total = 50;
        string json = JsonConvert.SerializeObject(result);
        return Content(json, "application/json");
    }

同样的错误。

【问题讨论】:

    标签: javascript asp.net-mvc json kendo-grid


    【解决方案1】:

    在使用 Telerik 支持交换电子邮件后,这是我发现和工作的:

    返回的 JSON 不能包含在 [] 中,尽管这是有效的 JSON。相反,以下形式有效。

    {
    "Data":
        [
            {"Name":"John","Description":"None"},
            {"Name":"Jane","Description":"StillNone"}
        ],
        "Total":50,"AggregateResults":null,"Errors":null
    }
    

    这就是DataSourceResult 返回的内容。一个同样有效的扁平字符串是:

    {
    "Total": "50",
    "Data": [
         {"NAME": "0720-FI-1081", "DESCRIPTION": "HP FLARE HEADER DISTRIBUTION"},
         {"NAME": "0720-FIC-1002","DESCRIPTION": null}
        ]
    }
    

    这里的重点是整个 JSON 结果不能被包裹在 [] 中,但是响应的 Data 字段必须被包裹在 [] 中。

    使用上面的字符串,网格声明必须从上面的 OP 中进行小修改:

    schema: {
            total: "Total",
            data: "Data"
        }
    

    因为这两个字段都是大写的。

    这在任何地方都没有记录,只是您必须知道的。

    干杯

    【讨论】:

    • “这在任何地方都没有记录,只是你必须知道的。”为什么每次我必须触摸剑道都会让我愤怒。
    【解决方案2】:

    我们也遇到过类似的情况,对我们来说唯一有效的解决方案是在返回时使用 DataSourceResult 对象,而不是返回搜索结果列表和总记录数。

    看看我们使用的代码:

      List<SearchResult> lst = GetSearchResult(filterCriteriaPassed);// Your search logic
    
      DataSourceResult result = null;
    
      // Reason to set the page as 1 for DataSource result was that we were only 
      // retriveing single page records so we had to set first page to show manually.
      request.Page = 1; 
      result = lst.ToDataSourceResult(request);
    
      result.Total = totalRecords;
    
      return Json(result);
    

    如果这不能解决您的问题,请告诉我,否则请将其标记为答案。

    【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2023-03-10
    • 2013-11-11
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多