开发者

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

开发者 https://www.devze.com 2023-02-09 03:22 出处:网络
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)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消