2015-07-03 44 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();