0

看到这个问题没有回应: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: "&nbsp;",
                    width: "70px"
                }
            ],
            editable: "inline",
            filterable: true,
            sortable: "true"
        });

我添加了该ID: {type: "number"},部分以尝试使其正常工作,我意识到我不应该在我的字段定义中需要它,因为它是用id: "ID".

4

1 回答 1

0

我弄清楚了问题所在,这是我提供的关于同一问题的另一个答案的文本(请参阅Kendo Grid does not update the grid with new created id after creation):

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

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}});
}
于 2013-11-15T17:50:44.677 回答