我在ASP.NET Web API中使用Kendo UI。有一個ProjectsController擁有所有必要的方法。Kendo UI DataSource沒有觸發transport.destroy
我的問題是,當我點擊Delete
按鈕,劍道UI電網將提高remove()
事件,但從未DataSource
調用transport.destroy
。相反,tansport.create
似乎正在被調用。在transport.parameterMap
我可以看到,操作是創建而不是銷燬。
下面是一個簡單的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: " ", width: "80px" }
],
remove: function (e) {
console.log("Delete button clicked.");
console.log("Project ID: " + e.model.ProjectId);
//dataSource.remove(e.model);
//dataSource.sync();
}
});
});
的Web API工作正常,但劍道UI窗格顯示:
POST http://localhost:port/api/Projects
當它應該是DELETE
。
謝謝大家提前!
其實並非如此。我可以看到對Web API的調用正在進行(不管我是否調用'sync()'或者如果'autoSync'設置爲true。我嘗試將batch設置爲false,但是這導致了另一個問題'transport.create '問題是刪除不會引發'transport.destroy',當我刪除一個項目時,它會一直提升'transport.create'。 –
我沒有看到你的代碼有什麼問題。試着看這篇文章OnaBai的回答解釋你的模型中的Id的需求,你似乎沒有定義:http://stackoverflow.com/questions/16662223/why-does-the-kendoui-grid-transport-create-event-gets-raised-multiple-times -and – Vojtiik
就是這樣,謝謝!在模型中添加id是解決方案 –