javascript - 使用 ajax 回调将数据源绑定(bind)到 Controller 时,数据未显示在 Kendo Grid 上

标签 javascript c# jquery ajax asp.net-mvc

我的问题有点类似于:

Binding Kendo Data Source with Async $.ajax calling from C# MVC Controller Action

这是创建 Kendo 网格的 javascript:

 $(document).ready(function () {
            $("#grid").kendoGrid({
                dataSource: getData(),
                height: 550,
                groupable: true,
                sortable: true,
                pageable: {
                    refresh: true,
                    pageSizes: true,
                    buttonCount: 10
                },
                columns: [{
                    field: "a",
                    title: "a",
                    width: 200
                }, {
                    field: "a",
                    title: "a"
                }]
            });
        });

这是我用来获取数据的 getData 函数,它是一个 ajax 回调函数,它调用 Controller 中名为 doSearch 的操作

 function getData() {
            jQuery.ajax({
                type: "POST",
                url: "@Url.Action("doSearch")",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ needSearch: true }),
                success: function (data) {
                    var dataSource = new kendo.data.DataSource({
                        data: JSON.parse(data)
                    });
                    dataSource.read();
                    return dataSource;
                },
                failure: function (errMsg) {
                    alert(errMsg);
                }
            });
        }

回调没问题,“数据”有值(value),但问题是:网格中没有显示数据。

这是数据的返回值:

 [{"a": "First record"},{"a": "Second record"}]

代码有什么问题吗?为什么数据不显示?

================================================== =================

以下是 getData 函数的更新代码:

function getData() {
 var grid = $("#grid").data("kendoGrid");
 grid.dataSource.transport.read.url = '@Url.Action("doSearch",new {needSearch = true})';
 grid.dataSource.read();
}

操作如下:

 public ActionResult doSearch(bool needSearch)
        {
            return Json("[{\"a\": \"First record\"},{\"a\": \"Second record\"}]");
        }

但是 Controller 中的 doSearch 操作没有触发

最佳答案

我尝试了你的方法,但这对我有用:

function getData() {
    var grid = $("#grid").data("kendoGrid");
    grid.dataSource.transport.options.read.url = '@Url.Action("doSearch", new {needSearch = true})';
    grid.dataSource.read();
}

备用:

$("#grid").kendoGrid({
    dataSource: {
        type: "json",
        transport: {
            read: '@Url.Action("doSearch", new {needSearch = true})'
        }
    }
);

注意:根据您的编辑,您的 Controller 方法需要更改为

public ActionResult doSearch(bool needSearch)
{
    return Json(new[] { new { a = "First record" }, new { a = "Second record" }};
}

关于javascript - 使用 ajax 回调将数据源绑定(bind)到 Controller 时,数据未显示在 Kendo Grid 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29645836/

相关文章:

javascript - jQuery AJAX - 将额外的键/值对推送到序列化的 $_POST 数组中

javascript - 如何在纯 JavaScript 中循环两种颜色?

javascript - 如何修改这个 jQuery 脚本,该脚本仅在用户向下滚动到它时才进行计数?

javascript - 巨型屏幕抓取/缩放

c# - COM 接口(interface)声明

javascript - 根据窗口大小增加属性的负值

jquery - 如何使用 kendo UI 核心创建日期选择器

c# - 加密随机唯一字符串

c# - 画棋盘

jquery - 第一列固定的表格滚动