arame3333
arame3333

Reputation: 10193

MVC: System.Web.HttpException: A public action method 'Delete' was not found on controller

I have a Html helper method that calls a Delete method on my controller.

public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave _leave)
{
    return html.RouteLink(linkText, "Default",
        new { _employeeOtherLeaveId = _leave.LeaveId, action = "Delete" },
        new { onclick = "$.post(this.href); return false;" });
}

On my controller I have;

[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult Delete(int _employeeOtherLeaveId)
{
    EmployeeOtherLeaf.Delete(_employeeOtherLeaveId);

    return RedirectToAction("Payroll");
}

But I get this runtime error message;

    System.Web.HttpException: A public action method 'Delete' was not found on controller

Upvotes: 2

Views: 3340

Answers (3)

Chris McGrath
Chris McGrath

Reputation: 1757

or you can also change your verb in your form method attribute to delete either method should work

Upvotes: 1

JMP
JMP

Reputation: 7834

It's possible that your action isn't being invoked by an HTTP request that is sending the correct "DELETE" verb in this case. Try updating your AcceptVerbs attribute to take a POST instead.

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038690

You are sending a POST request whereas your controller action expects a DELETE verb. So:

public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave _leave)
{
    return html.RouteLink(linkText, "Default",
        new { _employeeOtherLeaveId = _leave.LeaveId, action = "Delete" },
        new { onclick = "$.ajax({url: this.href, type: 'DELETE'}); return false;" });
}

Upvotes: 3

Related Questions