1

我正在使用带有 ASP.NET Web API 的 Kendo UI。有一个具有所有必要方法的 ProjectsController。

我的问题是,当我单击Delete按钮时,Kendo UI 网格会引发remove()事件,但从DataSource不调用transport.destroy. 相反,它似乎tansport.create正在被调用。在transport.parameterMap我可以看到该操作是create而不是destroy

这是一个示例 JavaScript 代码:

$(document).ready(function () {
    var apiUrl = '/api/projects/';
    var dataType = 'json';

    var dataSource = new kendo.data.DataSource({
        batch: true,
        autoSync: false,
        transport: {
            read: {
                url: apiUrl,
                dataType: dataType,
                type: "GET"
            },
            update: {
                url: apiUrl,
                dataType: dataType,
                type: "PUT"
            },
            destroy: {
                url: apiUrl,
                type: "DELETE"
            },
            create: {
                url: apiUrl,
                contentType: "application/json;charset=utf-8",
                dataType: dataType,
                type: "POST"
            },
            parameterMap: function (data, operation) {
                console.log("Operation: " + operation);
                if (operation === "create" && data.models) {
                    for (var i in data.models) {
                        var model = data.models[i];

                        if (model.ProjectId === 0) {
                            return kendo.stringify(model);
                        }
                    }
                } else if (operation === "destroy") {
                    console.log("Data.Models: " + data.models);
                    console.log("Data.id: " + data.ProjectId);
                    return { id: data.ProjectId };
                }

                return data;
            }
        },
        schema: {
            id: "ProjectId",
            model: {
                fields: {
                    ProjectId: { type: "number", editable: false, nullable: false, defaultValue: 0 },
                    ProjectName: { type: "string", validation: { required: true } },
                    Status: { type: "string", validation: { required: true } },
                    IsActive: { type: "boolean" }
                }
            }
        },
        pageSize: 10,
        serverPaging: false,
        serverFiltering: false,
        serverSorting: false
    });

    $("#projectsGrid").kendoGrid({
        dataSource: dataSource,
        groupable: false,
        sortable: true,
        pageable: {
            refresh: true,
            pageSizes: true
        },
        pageSize: 10,
        toolbar: ["create"],
        editable: "popup",
        columns: [
            { field: "ProjectId", width: 30, title: "ID" },
            { field: "ProjectName", width: 180, title: "Project" },
            { field: "Status", width: 90, title: "Status" },
            { field: "IsActive", width: 40, title: "Is Active", type: "boolean", template: '<input type="checkbox" #if (IsActive) {# checked="checked" #}# disabled="disabled" />' },
            { command: ["edit", "destroy"], title: "&nbsp", width: "80px" }
        ],

        remove: function (e) {
            console.log("Delete button clicked.");
            console.log("Project ID: " + e.model.ProjectId);
            //dataSource.remove(e.model);
            //dataSource.sync();
        }
    });
});

通过 Fiddler 发出请求时,Web API 工作正常,但 Kendo UI Grid 显示:

POST http://localhost:port/api/Projects

什么时候应该DELETE

提前谢谢大家!

4

2 回答 2

6

在您的数据源上,您将批处理标志设置为 true,这意味着数据源只有在您告诉它之后才会进行调用,例如调用 sync()。 http://docs.kendoui.c​​om/api/framework/datasource#configuration-batch

确保您已在模型中定义了 Id,正如 OnaBai 解释的这里为什么 KendoUI Grid Transport Create 事件会被多次引发,即使操作是更新?,您的 id 在模型之外,应该在:

   model: {
        id: "ProductID",
        fields: {
            ProductID: { editable: false, nullable: true },
        }
    }
于 2013-08-22T15:23:48.210 回答
4

如果有人按照上面的回答定义了idin model,但是 dataSource 还没有触发 transport.destroy ,下面的配置可能会有所帮助:

editable: {
..
mode: "inline", //or "popup
...
}
//or
editable: "inline" //or "popup"

http://www.telerik.com/forums/transport-destroy-of-grid-editor-is-not-working

于 2014-03-30T08:33:36.083 回答