删除,jqgrid中未设置任何URL [英] Delete, No url is set in jqgrid

查看:388
本文介绍了删除,jqgrid中未设置任何URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但是当我在警报中单击"Delete"按钮时,出现错误"No url is set".现在,我在ajax调用中有一个url来执行该功能.为什么会引发错误?

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编辑行期间也会发生这种情况.

解决方案

在我看来,您尝试以错误的方式使用Delete.您要做的是向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
});

I'm using jqgrid and when deleting a row in the grid i get the alert "Delete selected record?" and when i click ok i have written a code in onClickSubmit to make a ajax call to the controller which takes some parameters and delete the record. The functionality works fine.

But when i click "Delete" button in the alert i get an error "No url is set". Now, i have a url inside my ajax call which does the function. Why is the error thrown?

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

It works when i give some Dummy url in delete method which i dont need. I need another way to solve this.?

FYI, This happens for me also during the edit of a row using form editing.

解决方案

It seems to me that you try to use Delete in a wrong way. What you do is making Ajax request to '@Url.Action("DeleteSelectedTransactionAttachment", "Home")' with some additional data, do some unknown additional action inside of OnCompleteDeleteAttachments in case of successful deleting and do additional error handling in case of "Session TimeOut/UnAuthorized" error in statusText.

I think that correct implementation should looks more as the following

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

这篇关于删除,jqgrid中未设置任何URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆