Baxter
Baxter

Reputation: 5825

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. I am at the end of my rope on this, any help would be greatly appreciated.

Here is my dataSource declaration:

    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]
    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:

enter image description here

Using firebug the response header looks like this: enter image description here

The actual response looks like this:
enter image description here

However the grid does not display the data it looks like this: enter image description here

Note: For some reason the grid thinks it has 39 items in the bottom right corner.

Am I missing something obvious? Is there an easier way to do this?

Upvotes: 2

Views: 2821

Answers (2)

Soham
Soham

Reputation: 1281

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.

Upvotes: 1

Stef Heyenrath
Stef Heyenrath

Reputation: 9820

Use data: "d" instead of data: options.data ?

And add string as data type for the two fields in the schema definition.

Upvotes: 2

Related Questions