Asp.net mvc 4 剑道网格-创建传输,未设置ID

Asp.net mvc 4 剑道网格-创建传输,未设置ID,asp.net-mvc-4,kendo-ui,kendo-grid,Asp.net Mvc 4,Kendo Ui,Kendo Grid,请参见此问题,但没有回答: 创建新链接时,我的方法被点击,我返回ID设置为的完整项: [HttpPost] public JsonResult Create(string LINK_TEXT, string HREF, string CATEGORY) { var link = new LinkDTO {LINK_TEXT = LINK_TEXT, HREF = HREF, CATEGORY = CATEGORY};

请参见此问题,但没有回答:

创建新链接时,我的方法被点击,我返回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:

{“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: "&nbsp;",
                    width: "70px"
                }
            ],
            editable: "inline",
            filterable: true,
            sortable: "true"
        });
$(文档).ready(函数(){
$(“#链接结果”).kendoGrid({
数据源:{
运输:{
创建:{
url:'@url.Action(“创建”、“链接”),
数据类型:“json”,
类型:“POST”
},
阅读:{
url:'@url.Action(“读取”,“链接”),
数据类型:“json”,
键入:“GET”,
数据:{
类别:'@Model.CategoryName'
}
},
更新:{
url:'@url.Action(“更新”,“链接”),
数据类型:“json”,
键入:“POST”,
数据:{
类别:'@Model.CategoryName'
}
},
销毁:{
url:'@url.Action(“删除”、“链接”),
数据类型:“json”,
类型:“POST”
}
},
页面大小:10,
模式:{
数据:“链接”,
总计:“结果计数”,
型号:{
id:“id”,
字段:{
ID:{type:“number”},
链接文本:{type:“string”,必需:true},
HREF:{type:“string”,defaultValue:,验证:{required:true},
类别:{类型:“字符串”,默认值:'@Model.CategoryName',可编辑:false}
}
}
}
},
pageable:对,
工具栏:[“创建”],
栏目:[
{字段:“ID”,隐藏:true},
{字段:“链接文本”,标题:“名称”,宽度:“40px”/*,模板:“*/},
{字段:“HREF”,标题:“链接”,宽度:“100px”,可过滤:false/*,模板:“*/},
{字段:“类别”,标题:“类别”,宽度:“50px”,可过滤:false},
{
命令:[
{名称:“编辑”,文本:},
{名称:“销毁”,文本:}
],
标题:“,
宽度:“70px”
}
],
可编辑:“内联”,
可过滤:正确,
可排序:“正确”
});

我添加了
ID:{type:“number”},
部分,试图让它发挥作用,我意识到我不应该在字段定义中使用它,因为它是用
ID:“ID”
定义的。我发现了问题所在,下面是我提供的关于同一问题的另一个答案的文本(请参阅):

我遇到了同样的问题,我想我可能已经找到了答案。如果在架构中定义了保存结果的对象,则必须返回该对象中创建的链接的结果。示例:

schema: {
         data: "data",        
         total: "total",  .... 
 }
MVC方法示例:

public JsonResult CreateNewRow(RowModel rowModel)
{
    // rowModel.id will be defaulted to 0

    // save row to server and get new id back
    var newId = SaveRowToServer(rowModel);

    // set new id to model
    rowModel.id = newId;

    return Json(new {data= new[] {rowModel}});
}