Telerik 最近在他们的代码库中发布了一个解决方法:http ://www.kendoui.com/code-library/mvc/grid/save-all-changes-with-one-request.aspx 。不幸的是,解决方法相当简单。它提供了一个很好的示例,说明了如何捕获已损坏、脏记录和新记录,但最后需要挥手处理响应中的任何错误,并在成功时同步数据源。另请注意,在发出 ajax 请求之前,没有检查以确保有销毁、脏记录或新记录。
这是相关的代码。从上面的链接下载完整示例,查看网格的设置方式并确保您拥有最新版本。
function sendData() {
var grid = $("#Grid").data("kendoGrid"),
parameterMap = grid.dataSource.transport.parameterMap;
//get the new and the updated records
var currentData = grid.dataSource.data();
var updatedRecords = [];
var newRecords = [];
for (var i = 0; i < currentData.length; i++) {
if (currentData[i].isNew()) {
//this record is new
newRecords.push(currentData[i].toJSON());
} else if(currentData[i].dirty) {
updatedRecords.push(currentData[i].toJSON());
}
}
//this records are deleted
var deletedRecords = [];
for (var i = 0; i < grid.dataSource._destroyed.length; i++) {
deletedRecords.push(grid.dataSource._destroyed[i].toJSON());
}
var data = {};
$.extend(data, parameterMap({ updated: updatedRecords }), parameterMap({ deleted: deletedRecords }), parameterMap({ new: newRecords }));
$.ajax({
url: "/Home/UpdateCreateDelete",
data: data,
type: "POST",
error: function () {
//Handle the server errors using the approach from the previous example
},
success: function () {
alert("update on server is completed");
grid.dataSource._destroyed = [];
//refresh the grid - optional
grid.dataSource.read();
}
})
}