我很遗憾将网格重新绑定到从AJAX调用收到的数据。日期会丢失格式。
@{
ViewBag.Title = "Index";
}
@section Scripts {
<script type="text/javascript">
var viewModel;
$(function () {
// Load Data
$.ajax({
async: false,
url: "/Kendo/Home/GetData"
}).success(function (result) {
viewModel = new kendo.observable(result);
});
// Create Grid
$("#grid").kendoGrid({
dataSource: {
data: viewModel.People,
schema: {
model: {
fields: {
DOB: { type: "date" }
}
}
}
},
columns: [
{ field: "FirstName", title: "First Name" },
{ field: "LastName", title: "Last Name" },
{ field: "DOB", title: "Date of Birth", format: "{0:MM/dd/yyyy}" }
],
editable: true
});
$(document).on("click", "#btn-save", function (e) {
$.ajax({
url: "/Kendo/Home/Save",
type: "POST",
data: JSON.stringify(viewModel),
contentType: "application/json"
}).success(function (result) {
var g = $("#grid").data("kendoGrid");
viewModel = new kendo.observable(result);
g.dataSource.data(viewModel.People);
});
});
});
</script>
}
<div id="grid"></div>
<div id="view">
<input data-bind="value: firstName" />
<input data-bind="value: lastName" />
<button id="btn-save">Save</button>
</div>
加载的初始数据是:
{"People":[{"FirstName":"Sam","LastName":"Striano","DOB":"\/Date(215420400000)\/"},{"FirstName":"Karson","LastName":"Striano","DOB":"\/Date(321609600000)\/"}]}
从AJAX调用返回的数据(通过“保存”按钮)是:
{"People":[{"FirstName":"Sam 1234","LastName":"Striano","DOB":"\/Date(216288000000)\/"},{"FirstName":"Karson 1234","LastName":"Striano","DOB":"\/Date(322473600000)\/"}]}