显示从网格删除记录的确认消息 [英] Display a confirmation message on Delete a record from a Grid

查看:103
本文介绍了显示从网格删除记录的确认消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个确认信息时,用户从此我实现网格删除记录,但我有错误消息

通过记录下code被删除,但


  1. 仍然在网格我必须刷新记录,看看它disapear;

  2. 我有消息错误!即使记录被删除
    3。

      @ Html.ActionLink(删除学生,删除,新的{@StudentID = StudentID},{新类@ =glyphicon glyphicon铅笔,@ ID = StudentID })$(文件)。就绪(函数(){    $('a.delete')点击(OnDeleteClick)。});功能OnDeleteClick(五)
    {
        VAR StudentId = e.target.id;
        VAR标志=确认(你要永久删除此记录是否确定要删除这条记录?');    如果(标志){
            $阿贾克斯({
                网址:'/主页/ DeleteRecord',
                输入:POST,
                数据:{StudentID:StudentId},
                数据类型:JSON,
                成功:函数(结果){
                       警报(结果);
                       。$(#+ StudentId).parent()父()删除();
                       },
                错误:函数(){
                       警报('错误!');
                       }
            });
        }
        返回false;
    }


控制器:

 公众的ActionResult DeleteRecord(字符串StudentID)
    {
       // code删除
        }
        返回RedirectToAction(StudentGrid
                     家);
    }


解决方案

没有你正在使用尝试以下哪个网格错过:

获取最接近tr标签这样你就可以成功将其删除:

 变量$ TR = $(本).closest(TR);
$ tr.remove();

的jsfiddle

从您的控制器设置内容信息,重定向将无法工作,因为它是一个Ajax调用。

 公众的ActionResult DeleteRecord(字符串StudentID)
 {
    VAR成功= FALSE;
    // code删除
    //然后设置成功变量
    如果(成功)
    {
        返回的内容(已删除);
    }
    其他
    {
        返回的内容(失败);
    }
 }

然后从你的成功处理程序检查消息,并删除如果需要的话,客户端code最终会是这样的:

 函数OnDeleteClick(五)
{
    亦即preventDefault();
    变量$ TR = $(本).closest(TR);
    VAR StudentId = e.target.id;
    VAR标志=确认(你要永久删除此记录是否确定要删除这条记录?');    如果(标志){
        $阿贾克斯({
            网址:'/主页/ DeleteRecord',
            输入:POST,
            数据:{StudentID:StudentId},
            数据类型:JSON,
            成功:函数(结果){
                   如果(结果==已删除)
                        $ tr.remove();
                   },
            错误:函数(){
                   警报('错误!');
                   }
        });
    }
    返回false;
}

I want to display a confirmation message when the User delete a record from a grid this what I implement but I have the error message

With the code below the record is deleted but :

  1. the record still in the Grid I have to refresh to see it disapear;
  2. I Have the message Error! even if the record is deleted 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;
    }
    

Controller :

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

解决方案

Without seeing which grid you are using try the following:

Get the closest tr tag so you can remove it on success with:

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

jsFiddle

Set the content message from your controller, the Redirect won't work as it is an ajax call.

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

Then from your success handler check the message and remove if needed, the client-side code would end up like this:

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

这篇关于显示从网格删除记录的确认消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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