删除操作不会被调用

时间:2012-09-11 17:59:56

标签: asp.net-mvc

点击删除链接,我想在学生控制器中调用DeleteConfirmed Method。

学生管理员代码

[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存在。我不想显示删除视图而只是想要删除发生异步。

@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 个答案:

答案 0 :(得分:1)

在视图内而不是

@using (Html.BeginForm())

你应该

@using (Html.BeginForm("DeleteConfirmed", "YourControllerName"))

如果不起作用,删除ActionName("Delete")就可以了。

如果有帮助,请告诉我。