2013-07-26 51 views
4

我在很多地方看到過這個問題,但似乎找不到解決方案。所以我定義了一個帶有CRUD操作的Kendo網格,事情是之前被解僱的操作再次被解僱。 假設您刪除條目X,然後添加條目Y,創建操作將觸發,之後刪除操作(對於已刪除的X)將再次觸發。如果你先創建一個元素然後編輯另一個元素,那麼它也是同樣的事情,它編輯第二個元素,然後重新觸發第一個create語句並插入第一個插入元素的副本。如果您繼續進行多項操作,則會發生所有其他先前操作被觸發併發送給控制器的惡夢。Kendo UI Grid多次觸發CRUD操作

我網格是:

function InitializeIPAddressesGrid(userID) { 
    selectedUserID = userID; 
    $(".ipAddresses").kendoGrid({ 
     dataSource: IPAdressesDataSource, 
     sortable: { 
      mode: "single", 
      allowUnsort: false 
     }, 
     remove: function (e) { 
      this.refresh(); 
      var canDelete = confirm("Are you sure you want to delete this record?"); 
      if (!canDelete) { 
      e.preventDefault(); 
      } 

     }, 
     height: 420, 
     resizable: true, 
     pageable: { 
      refresh: true, 
      pageSize: 10 
     }, 
     selectable: "row", 
     toolbar: ["create"], 
     editable:{mode: "inline", confirmation:false} , 
     columns: [{ 
      field: "IpAddress", 
      title: "IP Address" 
     }, 
     { 
      field: "Status", 
      title: "Status" 
     }, 
     { 
      field: "LockedUntil", 
      title: "Locked until", 
      template: "#=kendo.toString(LockedUntil, 'yyyy/MM/dd')#" 
     }, 
      { command: ["edit", "destroy"], title: " ", width: "180px" } 
     ] 
    }); 

} 
var IPAdressesDataSource = new kendo.data.DataSource({ 
    type: "json", 
    serverPaging: true, 
    serverSorting: true, 
    serverFiltering: true, 
    pageSize: 10, 
    //scrollable:false, 
    transport: { 
     read: { 
      url: websiteRootUrl + '/PortalAuthorization/GetIPAddressesList', 
     }, 
     update: { 
      url: websiteRootUrl + "/PortalAuthorization/UpdateIP", 
      dataType: "json", 
      type: 'POST', 
      complete: function (e) { 
          if (e.status != 200) { 
           alert(eval('(' + e.responseText + ')').Message); 
          } 
          } 
      }, 
     create: { 
       url: websiteRootUrl + "/PortalAuthorization/CreateIP", 
       dataType: "json", 
       type: 'POST', 
       complete: function (e) { 
           if (e.status != 200) { 
            alert(eval('(' + e.responseText + ')').Message); 

           } 
           } 
       }, 
     destroy: { 
      url: websiteRootUrl + "/PortalAuthorization/DeleteIP", 
      dataType: "json", 
      type: 'DELETE', 
      complete: function (e) { 
          if (e.status != 200) { 
           alert(eval('(' + e.responseText + ')').Message); 
          } 
          } 
     }, 

     parameterMap: function (options, operation) { 

      if (operation == "update" && options) { 
       return {ipAddress: options.IpAddress , 
         status: options.Status , 
         lockedUntil: kendo.toString(options.LockedUntil, 'yyyy/MM/dd'), 
         pkey: options.ID, 
         databaseID: selectedDatabaseID }; 
      } 
      else 
      if (operation == "destroy" && options) 
      { 
       return { 
         databaseID: selectedDatabaseID, 
         pkey: options.ID, 
         userIDParam: selectedUserID 
         }; 
      } 
      else 
      if (operation == "create" && options) { 
       return {ipAddress: options.IpAddress , 
         status: options.Status , 
         lockedUntil: kendo.toString(options.LockedUntil, 'yyyy/MM/dd'), 
         pkey: options.ID, 
         userIDParam: selectedUserID, 
         databaseID: selectedDatabaseID }; 
      } 
      else 
      { 
      options.databaseID = selectedDatabaseID; 
      options.userID = selectedUserID; 
      return options; 
      } 
     } 
    }, 
    schema: { 
     model: { 
       id: "ID", 
       fields: { 
         IpAddress: { type: "string" }, 
         Status: { type: "string" }, 
         LockedUntil: { type: "date" } 
       } 
      },  
     data: function (data) { 
      return data.Items; 
     }, 
     total: function (data) { 
      return data.TotalCount; 
     } 
    } 
}); 

我的控制器:

public object UpdateIP(int databaseID, long pkey, string status, string lockedUntil, string ipAddress) 
    { 
      var database = [...]; 
     DynamicDataRepository repository = [...]; 
     string query = "..."; 

      repository.ExecuteNonQuery(query); 

     return new HttpResponseMessage(HttpStatusCode.OK); 
    } 

    public object DeleteIP(int databaseID, long pkey, int? userIDParam) 
    { 
     var database = [...]; 
     DynamicDataRepository repository = [...]; 
     string query = "..."; 

      repository.ExecuteNonQuery(query); 

     return new HttpResponseMessage(HttpStatusCode.OK); 
    } 

    public object CreateIP(int databaseID, long? pkey, string status, string lockedUntil, string ipAddress, int? userIDParam) 
    { 
     var database = [...]; 
     DynamicDataRepository repository = [...]; 
     string query = "..."; 

      repository.ExecuteNonQuery(query); 

     return new HttpResponseMessage(HttpStatusCode.OK); 
    } 

你有任何IDEEA?我做錯了什麼?提前致謝。附:控制器中的查詢工作正常。

+4

如果創建不夠,返回OK,您應該返回具有與默認值不同的ID的記錄。對於更新,您還應該返回元素以便刪除髒標誌。 – OnaBai

+0

我該怎麼做?我的意思是,在更新的情況下,我必須進行另一個查詢來獲取更新的數據行並將其作爲json傳遞給它? - 像在讀?另外,我應該在整個事件中初始化一些東西嗎?在刪除的情況下呢?..事情是我有另一種創建網格的方法,這是動態的..最壞的情況下,我會重做有問題的網格(我喜歡4-5網格完成「手動」和另外20個動態地完成的事情)就像那樣..但是這些動作也返回OK - 使用的動態。唯一的區別是它們在api控制器中。 – Vlad

+1

@OnaBai:對於更新,您不必返回元素,只返回已編輯元素的ID作爲響應正文就足夠了。 –

回答

3

我解決了這個問題,接着OnaBai建議返回更新/創建的實體,而在刪除的情況下,我返回了刪除條目的ID。

public object UpdateIP(int databaseID, long pkey, string status, string lockedUntil, string ipAddress) 
    { 
     var database = [...]; 
     DynamicDataRepository repository = [...]; 
     string query = [...]; 

     IPList updatedIP = new IPList { ID = pkey, IpAddress = ipAddress, Status = status, LockedUntil = DateTime.Today }; 

     return Json(updatedIP, JsonRequestBehavior.AllowGet); 

     // return new HttpResponseMessage(HttpStatusCode.OK); 
    } 

只有一個提到:在的情況下創建,方法似乎沒有工作,所以我所做的就是在創建操作我做了ipGrid.dataSource.read的.complete事件() ; ipGrid.refresh(); - 所以操作不會重複。 (我在這種情況下讀到,模型定義可能有問題 - 設置ID字段 - 但我確實設置了該字段)。非常感謝OnaBai