Reputation: 5825
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:
Using firebug the response header looks like this:
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.
Am I missing something obvious? Is there an easier way to do this?
Upvotes: 2
Views: 2821
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
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