简体   繁体   English

Kendo UI Grid WebMethod JSON

[英]Kendo UI Grid WebMethod JSON

I know that there are a lot of these out there already. 我知道已经有很多这样的东西了。 However, I have tried dozens of SO posts / forums / Kendo UI site and still can't get it to work. 但是,我已经尝试了数十个SO帖子/论坛/ Kendo UI网站,但仍然无法正常工作。 I am at the end of my rope on this, any help would be greatly appreciated. 我对此深信不疑,任何帮助将不胜感激。

Here is my dataSource declaration: 这是我的dataSource声明:

    var dataSource = new kendo.data.DataSource({
        transport: {
            read: function(options) {
                    $.ajax( {
                            type: "POST",
                            url:  "DepartmentHome.aspx/GetMembers",
                            data: options.data,
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (msg) {
                                options.success(msg.d);
                                alert(msg.d);
                            }
                        });
            }

        },
        pageSize: 20,
        schema: {
            model: {
                fields: {
                    FirstName: { validation: { required: true} },
                    LastName: { validation: { required: true} }
                }
            }
        }
    });

Here is my grid declaration using the data source: 这是我使用数据源的网格声明:

    $("#grid").kendoGrid({
        dataSource: dataSource,
        scrollable: true,
        groupable: false,
        sortable: true,
        pageable: {
            refresh: true,
            pageSizes: true
        },
        height: 430,
        toolbar: ["create"],
        columns: [
            { field: "FirstName", title: "First Name", width: "100px" },
            { field: "LastName", title: "Last Name", width: "100px" },
            { command: ["edit", "destroy"], width: "160px" }
            ],
        editable: {
            mode: "popup",
            confirmation: "Are you sure?"
        }
    });

Here is the code behind WebMethod I am calling in the datasource declaration: 这是我在数据源声明中调用的WebMethod背后的代码:

    [WebMethod]
    public static string GetMembers()
    {
        var serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(new { FirstName = "John", LastName = "Smith" });
        return json; 
    }

I know it is hitting the WebMethod because the alert I put in the data source shows the correct data: 我知道它正在击中WebMethod,因为我放入数据源中的警报显示了正确的数据:

在此处输入图片说明

Using firebug the response header looks like this: 使用firebug,响应头看起来像这样: 在此处输入图片说明

The actual response looks like this: 实际响应如下所示:
在此处输入图片说明

However the grid does not display the data it looks like this: 但是,网格不会显示看起来像这样的数据: 在此处输入图片说明

Note: For some reason the grid thinks it has 39 items in the bottom right corner. 注意:由于某种原因,网格认为它在右下角有39个项目。

Am I missing something obvious? 我是否缺少明显的东西? Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

Use data: "d" instead of data: options.data ? 使用data:“ d”代替data:options.data吗?

And add string as data type for the two fields in the schema definition. 并将字符串作为模式定义中两个字段的数据类型添加。

Your code looks fine. 您的代码看起来不错。 Just one change you need in your success function. 成功功能中只需要一项更改。

Change your code as given below: 更改您的代码,如下所示:

success: function (msg) {
    options.success($.parseJSON(msg.d));
}

$.parseJSON will convert your json string into object. $ .parseJSON将您的json字符串转换为对象。

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

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