2013-08-22 105 views
1

我在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: "&nbsp", 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

謝謝大家提前!

回答

6

在您的數據源上,您將批處理標誌設置爲true,這意味着數據源僅在您告訴它之後纔會進行調用,例如調用sync()。 http://docs.kendoui.com/api/framework/datasource#configuration-batch

除了

確保您已在模型中定義ID,如OnaBai這裏Why does the KendoUI Grid Transport Create event gets raised multiple times, and even when the action is Update?解釋,你的ID是模型之外,應該是:

model: { 
     id: "ProductID", 
     fields: { 
      ProductID: { editable: false, nullable: true }, 
     } 
    } 
+0

其實並非如此。我可以看到對Web API的調用正在進行(不管我是否調用'sync()'或者如果'autoSync'設置爲true。我嘗試將batch設置爲false,但是這導致了另一個問題'transport.create '問題是刪除不會引發'transport.destroy',當我刪除一個項目時,它會一直提升'transport.create'。 –

+0

我沒有看到你的代碼有什麼問題。試着看這篇文章OnaBai的回答解釋你的模型中的Id的需求,你似乎沒有定義:http://stackoverflow.com/questions/16662223/why-does-the-kendoui-grid-transport-create-event-gets-raised-multiple-times -and – Vojtiik

+0

就是這樣,謝謝!在模型中添加id是解決方案 –

4

,如果有人有定義idmodel中的回答如上,但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

+0

對我來說也是如此 - 如果editable = false,銷燬不會被觸發+1 –

+0

謝謝Boss .. Wat 3-4小時由於這個問題.. – Coder