Rishikesh
Rishikesh

Reputation: 496

Delete Action doesn't get called

On click of delete link i want to call DeleteConfirmed Method in my Student Controller.

Student Controller Code

[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 has delete link (last line of code) on click of which i want the DeleteConfirmed to be called but the below expects delete.cshtml to be present.I don't want to show delete view and just want the delete to happen asynhronously.

@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>

}

}

Upvotes: 0

Views: 895

Answers (1)

Display Name
Display Name

Reputation: 4732

Inside the view instead of

@using (Html.BeginForm())

You should have

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

If won't work, removing ActionName("Delete") should do the trick.

Let me know if that was of help.

Upvotes: 1

Related Questions