看到这个问题没有回应:Kendo Grid does not update the grid with new created id after creation
创建新链接时,我的方法被命中,我返回带有 ID 集的完整项目:
[HttpPost]
public JsonResult Create(string LINK_TEXT, string HREF, string CATEGORY)
{
var link = new LinkDTO {LINK_TEXT = LINK_TEXT, HREF = HREF, CATEGORY = CATEGORY};
var insertedLink = LinkService.Create(link);
return Json(new[] {insertedLink}, JsonRequestBehavior.AllowGet);
}
在客户端,响应是:
{"ID":86,"LINK_TEXT":"Test2","HREF":"http://www.google.com","CATEGORY":"Category1"}
我也试过只返回 ID:
{“身份证”:86}
检查剑道数据后:
CATEGORY: "Category1"
HREF: "http://www.google.com"
ID: 0
LINK_TEXT: "Test2"
_events: Object
dirty: false
id: 0
parent: function (){return r}
uid: "4739cc3d-a270-44dd-9c63-e9d378433d98"
ID 为 0。当我尝试编辑此链接时,它认为它是新链接,因此它再次调用 create,从而复制了链接。
这是我的网格定义:
$(document).ready(function () {
$("#link-results").kendoGrid({
dataSource: {
transport: {
create: {
url: '@Url.Action("Create", "Link")',
dataType: 'json',
type: 'POST'
},
read: {
url: '@Url.Action("Read", "Link")',
dataType: 'json',
type: 'GET',
data: {
CATEGORY: '@Model.CategoryName'
}
},
update: {
url: '@Url.Action("Update", "Link")',
dataType: 'json',
type: 'POST',
data: {
CATEGORY: '@Model.CategoryName'
}
},
destroy: {
url: '@Url.Action("Delete", "Link")',
dataType: 'json',
type: 'POST'
}
},
pageSize: 10,
schema: {
data: "Links",
total: "ResultCount",
model: {
id: "ID",
fields: {
ID: {type: "number"},
LINK_TEXT: { type: "string", required: true },
HREF: { type: "string", defaultValue: "", validation: { required: true } },
CATEGORY: { type: "string", defaultValue: '@Model.CategoryName', editable: false }
}
}
}
},
pageable: true,
toolbar: ["create"],
columns: [
{ field: "ID", hidden: true },
{ field: "LINK_TEXT", title: "Name", width: "40px"/*, template: "<a href='#=HREF#'>#=LINK_TEXT#</a>"*/ },
{ field: "HREF", title: "Link", width: "100px",filterable: false/*, template: "<a href='#=HREF#'>#=HREF#</a>"*/ },
{ field: "CATEGORY", title: "Category", width: "50px", filterable: false},
{
command: [
{ name: "edit", text: "" },
{ name: "destroy", text: "" }
],
title: " ",
width: "70px"
}
],
editable: "inline",
filterable: true,
sortable: "true"
});
我添加了该ID: {type: "number"},
部分以尝试使其正常工作,我意识到我不应该在我的字段定义中需要它,因为它是用id: "ID"
.