删除,没有在jqgrid中设置url

时间:2014-11-21 11:00:00

标签: asp.net-mvc jqgrid

我正在使用jqgrid,当删除网格中的一行时,我会收到警告" Delete selected record?"当我点击确定时,我在onClickSubmit中编写了一个代码,对控制器进行ajax调用,该控制器接受一些参数并删除记录。功能正常。

但是当我点击" Delete"警报中的按钮我收到错误" No url is set"。现在,我在url调用中有一个ajax来完成该功能。为什么会抛出错误?

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

当我在删除方法中提供一些我不需要的虚拟URL时,它有效。我需要另一种解决方法。

仅供参考,在使用form editing修改行时也会发生这种情况。

1 个答案:

答案 0 :(得分:2)

在我看来,您尝试以错误的方式使用Delete。您所做的是向'@Url.Action("DeleteSelectedTransactionAttachment", "Home")'发送带有一些其他数据的Ajax请求,在成功删除的情况下在OnCompleteDeleteAttachments内部执行一些未知的附加操作,并在发生"Session TimeOut/UnAuthorized"错误时执行其他错误处理statusText

我认为正确的实现应该更像以下

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