Kendo UI grid triggers CRUD operations several times

I have seen this problem in many places, but I cannot find a solution. Therefore, I defined a Kendo grid with CRUD operations that the previous running operations started again. Let's say you delete the X record, and then add the Y record, the create operation starts, and then the delete operation (for X, which was deleted) works again. The same thing if you first create an element and then edit another, edit the second element and then re-run the first create statement and insert a duplicate for the first inserted element. If you turn on with multiple operations, the nightmare happens with all other previous operations that are started and sent to the controller.

My grid:

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; } } }); 

My controllers:

  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); } 

Do you have an idea? where did i do something wrong? thanks in advance. Postscript requests in controllers work fine.

+4
source share
1 answer

I fixed this problem after OnaBai suggested returning the "Updated / Created" object, and in the case of "Delete" I returned the identifier of the deleted record.

  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); } 

Just one mention: in the case of CREATE, the method does not seem to work, so I did it in the .complete event of the CREATE operation, I did ipGrid.dataSource.read (); ipGrid.refresh (); - therefore, the operation is not repeated. (I read that in this case there may be a problem with the definition of the model - setting the ID field, but I set it). Thanks a lot OnaBai

+3
source

Source: https://habr.com/ru/post/1493688/


All Articles