Delete, URL not set in jqgrid

I use jqgrid , and when I delete a row in the grid, I get a warning “ Delete selected record? ”, And when I click ok, I wrote code in onClickSubmit to make an ajax controller call that takes some parameters and deletes the record. Functionality is working fine.

But when I click the “ Delete ” button in the warning, I get the error message “ No url is set ”. Now I have a url inside my ajax call that performs this function. Why was the error reset?

jqGrid:

 var selectedRowId = "125"; $("#AttachmentsGrid").jqGrid({ url: '@Url.Action("LoadTransactionAttachments", "Home")', postData: { 'transactionId': selectedRowId }, mtype: 'GET', datatype: 'json', jsonReader: { id: 'AttachmentId', repeatitems: false }, height: 'auto', hidegrid: false, rownumbers: true, autowidth: true, shrinkToFit: false, rowNum: 10, pager: '#AttachmentsPager', caption: "Attachments", colNames: ['AttachmentName'], colModel: [{ name: 'AttachmentName', index: 'AttachmentName', formatter: imageFormatter, unformat: imageUnFormatter }], beforeRequest: function () { responsive_jqgrid($(".jqGrid")); }, onSelectRow: function (id) { var statusId; attachmentId = id; var selectValues = jQuery('#AttachmentsGrid').jqGrid('getRowData', id); attachmentName = selectValues.AttachmentName; if (accessLevel.HasDeleteAttachmentAccess == true) $("#del_AttachmentsGrid").show(); else $("#del_AttachmentsGrid").hide(); }, loadComplete: function () { UnBlockUI(); } }); jQuery("#AttachmentsGrid").jqGrid('navGrid', '#AttachmentsPager', { edit: false, add: false, del: true, search: false, refresh: true, refreshtext: "" }, {}, {}, { // url: '@Url.Action("UpdateDummyData", "Home")', // Delete attachment event. onclickSubmit: function (response, postData) { $.ajax({ url: '@Url.Action("DeleteSelectedTransactionAttachment", "Home")', datatype: 'json', data: { 'attachmentId': JSON.stringify(postData), 'attachmentName': attachmentName, 'transactionId': selectedRowId }, type: 'POST', success: OnCompleteDeleteAttachments, error: function (xhr, status, error) { if (xhr.statusText == "Session TimeOut/UnAuthorized") { alert(xhr.statusText); window.location.href = '@Url.Action("LogOut", "Account")'; } else alert(xhr.responseText); } }); 

This works when I give some Dummy url in the delete method that I don't need. I need another way to solve this problem.

FYI . This also happens during line editing with form editing .

0
source share
1 answer

It seems to me that you are trying to use Delete incorrectly. You make an Ajax request to '@Url.Action("DeleteSelectedTransactionAttachment", "Home")' with some additional data, perform some unknown additional actions inside OnCompleteDeleteAttachments in case of successful deletion, and perform additional error handling in case of a "Session TimeOut/UnAuthorized" in statusText .

I think the correct implementation should look like this

 jQuery("#AttachmentsGrid").jqGrid('navGrid', '#AttachmentsPager', { edit: false, add: false, search: false, refreshtext: "" }, {}, {}, { url: '@Url.Action("DeleteSelectedTransactionAttachment", "Home")', serializeDelData: function (postData) { return { attachmentId: JSON.stringify(postData), attachmentName: attachmentName, transactionId: selectedRowId } }, errorTextFormat: function (xhr) { if (xhr.statusText == "Session TimeOut/UnAuthorized") { window.location.href = '@Url.Action("LogOut", "Account")'; } else { return xhr.responseText; } }, afterSubmit: OnCompleteDeleteAttachments }); 
+2
source

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


All Articles