Use DELETE form method in Html.BeginForm()?
Asked Answered
D

1

8

I'd like to use the appropriate HTTP method when possible. In this case, when a button is clicked to delete something, I want to fire the controller action with the attribute [HttpDelete]. However, I can't seem to create a form with this method - using Razor syntax. The FormMethod enum does not have an option for Delete and doing the following doesn't override it:

@using (Html.BeginForm("Order", "Users", FormMethod.Post, new { method = "DELETE" }))

Searching for solutions yields none, is nobody doing this? I know I can just use POST but isn't this the point of the HTTP delete method to begin with?

Disadvantage answered 12/1, 2012 at 12:58 Comment(1)
Joeri Jans is correct. In MVC, you need to trick the action method into thinking the POST is a DELETE (or PUT).Munt
W
23

You need this in your form:

@using (Html.BeginForm("Order", "Users"){ 
   @Html.HttpMethodOverride(HttpVerbs.Delete)
}
Woad answered 12/1, 2012 at 13:3 Comment(4)
Extra information: The HTML spec doesn't permit anything else than "get" or "post" as the method for forms, so MVC has to check for the hidden input that HttpMethodOverride creates and then assume that this verb was sent.Corriecorriedale
Thanks JoJa. Christian, if it's not in the HTML spec then I assume I should just stick with POST...?Disadvantage
HTML5 spec supports DELETE and PUTFarant
HTML5 actual spec doesn't, it was removed from the draft unfortunately.Pool

© 2022 - 2024 — McMap. All rights reserved.