简体   繁体   English

使用ajax回调将数据源绑定到Controller时,数据未在Kendo网格上显示

[英]Data isn't showing on Kendo Grid When Binding Data Source with ajax callback to Controller

My question is a bit similar to this: 我的问题与此类似:

Binding Kendo Data Source with Async $.ajax calling from C# MVC Controller Action 将Kendo数据源与从C#MVC控制器操作调用的异步$ .ajax绑定

Here's the javascript for creating the Kendo grid: 这是用于创建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"
                }]
            });
        });

Here's the getData function which I used to get data, it's an ajax callback function which calls an action in the controller called doSearch 这是我用来获取数据的getData函数,它是一个ajax回调函数,它在名为doS​​earch的控制器中调用一个动作

 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);
                }
            });
        }

The callback is fine, and the 'data' has value, but the problem is: there is no data shown in the grid. 回调很好,并且“数据”具有值,但是问题是:网格中没有显示数据。

Here's the returned value for data: 这是数据的返回值:

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

What's wrong with the code? 代码有什么问题? Why isn't the data showing? 为什么没有显示数据?

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

Here's the updated code for getData function: 这是getData函数的更新代码:

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

And here's the action: 这是动作:

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

But the doSearch Action in the Controller isn't firing 但是Controller中的doSearch操作没有触发

I tried your way, but this works for me: 我尝试了您的方式,但这对我有用:

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

alternate: 备用:

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

Note: Based on your edit, your controller method needs to be changed to 注意:根据您的编辑,您的控制器方法需要更改为

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

check this 检查这个

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) {
                    return data;
                },
                failure: function (errMsg) {
                    alert(errMsg);
                }
            });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM