Use DELETE form method in Html.BeginForm()?

2019-02-17 03:52发布

问题:

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?

回答1:

You need this in your form:

@using (Html.BeginForm("Order", "Users"){ 
   @Html.HttpMethodOverride(HttpVerbs.Delete)
}