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
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.
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;" });
}
or you can also change your verb in your form method attribute to delete either method should work
精彩评论