【问题标题】:Display a confirmation message on Delete a record from a Grid在从网格中删除记录时显示确认消息
【发布时间】:2015-07-03 10:00:03
【问题描述】:

当用户从网格中删除记录时,我想显示一条确认消息,这是我实现的,但我有错误消息

使用下面的代码,记录被删除但是:

  1. 记录仍在网格中,我必须刷新才能看到它消失;
  2. 我收到消息错误!即使记录被删除 3.

     @Html.ActionLink("Delete Student", "Delete", new { @StudentID = StudentID }, new { @class="glyphicon glyphicon-pencil", @id=StudentID })
    
    $(document).ready(function () {
    
        $('a.delete').click(OnDeleteClick);
    
    });
    
    function OnDeleteClick(e)
    { 
        var StudentId = e.target.id; 
        var flag = confirm('You are about to delete this record permanently. Are you sure you want to delete this record?');
    
        if (flag) { 
            $.ajax({
                url: '/Home/DeleteRecord',
                type: 'POST', 
                data: { StudentID: StudentId },
                dataType: 'json', 
                success: function (result) { 
                       alert(result); 
                       $("#" + StudentId).parent().parent().remove(); 
                       },
                error: function () { 
                       alert('Error!'); 
                       }
            });
        }
        return false;
    }
    

控制器:

    public ActionResult DeleteRecord(string StudentID)
    {
       //Code to delete
        }
        return RedirectToAction("StudentGrid",
                     "Home");
    }

【问题讨论】:

    标签: c# jquery asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    如果没有看到您使用的是哪个网格,请尝试以下操作:

    获取最近的 tr 标签,以便在成功时删除它:

    var $tr = $(this).closest("tr");
    $tr.remove();
    

    jsFiddle

    从您的控制器设置内容消息,重定向将不起作用,因为它是一个 ajax 调用。

     public ActionResult DeleteRecord(string StudentID)
     {
        var success = false;
        //Code to delete
        // then set success variable
        if (success)
        {
            return Content("Deleted");
        }
        else
        {
            return Content("Failed");
        }          
     }
    

    然后从您的成功处理程序中检查消息并在需要时删除,客户端代码将像这样结束:

    function OnDeleteClick(e)
    { 
        e.preventDefault();
        var $tr = $(this).closest("tr");  
        var StudentId = e.target.id; 
        var flag = confirm('You are about to delete this record permanently. Are you sure you want to delete this record?');
    
        if (flag) { 
            $.ajax({
                url: '/Home/DeleteRecord',
                type: 'POST', 
                data: { StudentID: StudentId },
                dataType: 'json', 
                success: function (result) { 
                       if (result == "Deleted")
                            $tr.remove();  
                       },
                error: function () { 
                       alert('Error!'); 
                       }
            });
        }
        return false;
    }
    

    【讨论】:

    • 我正在使用 GridMVC 你的代码为消息工作,但网格没有刷新
    • @melom 抱歉,我不确定如何刷新 GridMVC。我会提出另一个专门针对此的问题。我敢肯定,有人会很快回答,我已经很久没有使用它了。 :)
    【解决方案2】:
       public ActionResult DeleteRecord(string StudentID)
        {
            //Code to delete
        }
         return Json("Record Is Delete", JsonRequestBehavior.AllowGet); 
        }
    

    是控制器的响应,您可以在 alert() 中显示此 MSG 在项目中使用更新网格,您可以使用下面的代码就足够了

    $(e).closest("tr").remove();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 2019-12-14
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      相关资源
      最近更新 更多