我是kendo网格的新来者,我使用webmethod来触发它。在很多论坛的帮助下,我终于可以触发webmethod的请求了。问题是我的方法返回JSON格式的记录数。为简单起见,我认为这里有一个硬编码的JSON记录是响应:
{"d":"{\"pk_Picture\":22,\"P_DisplayOrder\":1,\"AltAttribute\":\"Smith\",\"TitleAttribute\":\"Smith\"}"}
<script>
$(document).ready(function () {
$("#productpictures-grid").kendoGrid({
height: 200,
columns: [
{ field: "pk_Picture", title: "Picture", width: "150px" },
{ field: "P_DisplayOrder", width: "150px" },
{ field: "AltAttribute", width: "100px" },
{ field: "TitleAttribute", width: "100px" },
{ command: "destroy", title: "Delete", width: "110px" }
],
pageable: {
info: true
}, // enable paging
//filterable: true, // enable filtering
//sortable: true, // enable sorting
editable: true, // enable editing
//toolbar: ["create", "save", "cancel"], // specify toolbar commands
dataSource: {
serverPaging: true,
serverSorting: true,
serverFiltering: true,
pageSize: 10,
schema: {
data: "d.results", // web methods return JSON in the following format { "d": <result> }. Specify how to get the result.
total: "d.Total",
model: { // define the model of the data source. Required for validation and property types.
fields: {
pk_Picture: { editable: false, type: "string" },
P_DisplayOrder: { editable: true, type: "string" },
AltAttribute: { editable: true, type: "string" },
TitleAttribute: { editable: true, type: "string" }
}
}
},
// batch: true, // enable batch editing - changes will be saved when the user clicks the "Save changes" button
transport: {
create: {
url: "TestPage.aspx/Create", //specify the URL which should create new records. This is the Create method of the Products.asmx service.
contentType: "application/json; charset=utf-8", // tells the web method to serialize JSON
type: "POST" //use HTTP POST request as the default GET is not allowed for web methods
},
read: {
url: "Update.aspx/FillProductDataById", //specify the URL which data should return the records. This is the Read method of the Products.asmx service.
contentType: "application/json; charset=utf-8", // tells the web method to serialize JSON
type: "POST" //use HTTP POST request as the default GET is not allowed for web methods
},
update: {
url: "TestPage.aspx/Update", //specify the URL from which should update the records. This is the Update method of the Products.asmx service.
contentType: "application/json; charset=utf-8", // tells the web method to serialize JSON
type: "POST" //use HTTP POST request as the default GET is not allowed for web methods
},
destroy: {
url: "TestPage.aspx/Destroy", //specify the URL which should destroy records. This is the Destroy method of the Products.asmx service.
contentType: "application/json; charset=utf-8", // tells the web method to serialize JSON
type: "POST" //use HTTP POST request as the default GET is not allowed for web methods
},
parameterMap: function (data, operation) {
if (data.models) {
return JSON.stringify(data);
return JSON.stringify({ products: data.models });
} else if (operation == "read") {
//Page methods always need values for their parameters
data = $.extend({ ProductIds: $('#Id').val() }, data);
return JSON.stringify(data);
}
}
}
}
});
});
</script>
这是我的服务器方法
[WebMethod]
public static string FillProductDataById(string ProductIds)
{
BLProduct objProduct = new BLProduct();
string json = JsonConvert.SerializeObject(new { pk_Picture = 22, P_DisplayOrder = 1, AltAttribute = "Smith", TitleAttribute = "Smith" });
return json;
//DataTable dt = new DataTable();
//dt = objProduct.GetProductByProductId(ProductIds);
//return JsonConvert.SerializeObject(dt);
}
请帮忙
答案 0 :(得分:0)
从服务器返回的JSON似乎不是数组。 从服务器到kendo的JSON数据应该是一个数组。
例如:
[{"pk_Picture":22,"P_DisplayOrder":1,"AltAttribute":"Smith","TitleAttribute":"Smith"},
{{"pk_Picture":33,"P_DisplayOrder":2,"AltAttribute":"Hamed","TitleAttribute":"Hamed"}]
这样做:
[WebMethod]
public static string FillProductDataById(string ProductIds)
{
BLProduct objProduct = new BLProduct();
DataTable dt = new DataTable();
dt = objProduct.GetProductByProductId(ProductIds);
var dataArray = dt.AsEnumerable().ToArray();
return JsonConvert.SerializeObject(dataArray);
}