【发布时间】:2016-07-15 20:59:07
【问题描述】:
我是剑道网格的新手,我正在使用 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);
}
请帮忙
【问题讨论】:
标签: jquery asp.net json webforms kendo-grid