Search code examples
asp.net-mvchtml.actionlink

MVC Delete Action Link Not Working


My Action link looks like so:

<%= Html.ActionLink("Delete Message", "DeleteMessage", new { messageId=item.MESSAGEID })%>

And my Action in my controller looks like so:

    [AcceptVerbs(HttpVerbs.Delete)]
    public ActionResult DeleteMessage(int messageId)
    {

        Message message = context.Messages.FirstOrDefault(m => m.MESSAGEID.Equals(messageId));
        if (message != null)
        {
            context.Messages.DeleteOnSubmit(message);
            context.SubmitChanges();
        }
        return View();
    }

But for some reason, the item in my model still does not get deleted. Where have I gone wrong?


Solution

  • ActionLink produces an a element with a link which is accessed with standard GET request. Your action, however, explicitly specifies that it expects DELETE method, hence it never actually gets invoked.

    Moreover, no "destructive" actions should be performed with GET requests. See this on implementing "delete" link.