2015-07-03 36 views
1

我想显示一个确认信息时,用户从这个我实现网格删除记录删除记录的确认消息,但我有错误消息显示器上对来自网格

与记录下面的代码删除但:

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

回答

0

没有你正在使用尝试哪个网格看到以下内容:

获取最接近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; 
} 
+0

我正在使用网格MVC你的代码工作的消息,但网格不刷新 – melom

+0

@melom对不起,我不知道如何刷新GridMVC。我会创建另一个具体的问题。有人会很快回答我确信,我已经很久没有使用它了。 :) – hutchonoid

0
public ActionResult DeleteRecord(string StudentID) 
    { 
     //Code to delete 
    } 
    return Json("Record Is Delete", JsonRequestBehavior.AllowGet); 
    } 

与来自控制器的响应可以显示这个MSG在alert() 与更新网格项目中您可以使用下面的代码是足够的

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