2014-11-21 61 views
0

設置我使用jqgrid並在網格中的一行,當我得到警報「Delete selected record?」當我點擊確定我已經寫在onClickSubmit代碼做出ajax調用控制器會接收一些參數並刪除記錄。該功能正常工作。刪除,任何URL中的jqGrid

但是,當我在警報中,單擊「Delete」按鈕,我得到一個錯誤「No url is set」。現在,我有一個urlajax調用,它確實在函數內部。爲什麼會拋出錯誤?

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

它工作時,我給在刪除方法,我不需要一些虛擬的網址。我需要另一種方法來解決這個問題。

FYI,在使用form editing編輯一行時,這也發生在我身上。

回答

2

在我看來,你嘗試使用一個錯誤的方式刪除。你做什麼,正在Ajax請求'@Url.Action("DeleteSelectedTransactionAttachment", "Home")'一些其他數據,做OnCompleteDeleteAttachments內的一些未知的額外行動,成功刪除的情況下,做更多的錯誤statusText"Session TimeOut/UnAuthorized"錯誤的情況下處理。

我認爲正確的實現應該看起來更加爲以下

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

這是非常甜蜜。完美的工作。謝謝!!! – iamCR 2014-11-24 11:58:20

+0

@SanthoshKumar:歡迎您! – Oleg 2014-11-24 12:17:10