我有一个Html辅助方法,可以在我的控制器上调用Delete方法。
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;" });
}
我的控制器上有;
[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult Delete(int _employeeOtherLeaveId)
{
EmployeeOtherLeaf.Delete(_employeeOtherLeaveId);
return RedirectToAction("Payroll");
}
但我收到此运行时错误消息;
System.Web.HttpException: A public action method 'Delete' was not found on controller
答案 0 :(得分:5)
在这种情况下,发送正确“DELETE”动词的HTTP请求可能不会调用您的操作。尝试更新AcceptVerbs属性以取代POST。
答案 1 :(得分:3)
您正在发送POST请求,而您的控制器操作需要DELETE谓词。所以:
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;" });
}
答案 2 :(得分:1)
或者您也可以在表单方法属性中更改动词以删除任何一种方法