2012-09-11 70 views
0

點擊刪除鏈接我想在我的學生控制器中調用DeleteConfirmed方法。刪除操作不會被調用

學生控制器代碼

[HttpPost, ActionName("Delete")] 
    public ActionResult DeleteConfirmed(int id) 
    { 
     Student student = db.Students.Find(id); 
     db.Students.Remove(student); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

Index.cshtml有刪除鏈接(代碼最後一行)上點擊其中的我想DeleteConfirmed被稱爲但低於預期delete.cshtml是present.I唐不想顯示刪除視圖,只想刪除發生異步。

@model IEnumerable<SMS.Model.Student> 
@{ 
ViewBag.Title = "Student"; 
} 


<h2>Student</h2> 
@using (Html.BeginForm()) { 

<p> 
@Html.ActionLink("Create New", "Create") 
</p> 
<table> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.RegistrationNo) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.FirstName) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.MiddleName) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.LastName) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.DateofBirth) 
    </th> 
    <th></th> 
</tr> 

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.RegistrationNo) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.FirstName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.MiddleName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.LastName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.DateofBirth) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | 
     @Html.ActionLink("Details", "Details", new { id=item.Id }) | 
     @Ajax.ActionLink("Delete", "Delete", new { id = item.Id },new AjaxOptions { HttpMethod = "Post"}) 

    </td> 
</tr> 

}

}

回答

1

的觀點,而不是

@using (Html.BeginForm()) 

裏面你應該有

如果不行,刪除ActionName("Delete")應該訣竅。

讓我知道這是否有幫助。

+0

沒有..它不起作用。它仍然調用刪除而不是後,給出錯誤資源找不到。 描述:HTTP 404.您正在查找的資源(或其某個依賴項)可能已被刪除,名稱已更改或暫時不可用。請檢查以下網址並確保它拼寫正確。 請求的URL:/學生/刪除/ 5 – Rishikesh

+0

嘗試用''Ajax.ActionLink(「Delete」,「Delete」,new {id = item.Id},new AjaxOptions {HttpMethod =「Post」}) @Ajax.ActionLink(「Delete」,「DeleteConfirmed」,new {id = item.Id},new AjaxOptions {HttpMethod =「Post」})'帶有和不帶ActionName(「Delete」)'在行動之上。 –

+0

請問您對此有任何幫助嗎?謝謝。 –

相關問題