Reputation: 2441
In my controller I tried something like that:
[HttpDelete, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Movie movie = db.Movies.Find(id);
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
After clicking, steering doesn't visit the method above. If there is something missing? Maybe the problem is in the view with html code. I am using standard form such as:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<p>
<input type="submit" value="Delete" formmethod="delete"/> |
@Html.ActionLink("Back to List", "Index")
</p>
}
I tried also Html.BeginForm("Delete", "Action", FormMethod.Delete)
but it isn't supported by IntelliSense (it shows that there are only FormMethod.Get and FormMethod.Post).
As far as I know, the Delete method is using exactly to manipulate objects to remove from database. Could you explain me if I am right? If not - why? If yes - tell me, please, how to implement that method good way.
Upvotes: 0
Views: 265
Reputation: 78595
Add a HttpMethodOverride to your BeginForm()
call:
@using (Html.BeginForm()) {
@Html.HttpMethodOverride(HttpVerbs.Delete)
@Html.AntiForgeryToken()
<p>
<input type="submit" value="Delete" formmethod="delete"/> |
@Html.ActionLink("Back to List", "Index")
</p>
}
Upvotes: 3
Reputation: 68440
I don't think it's necessary to restrict your action to HttpDelete
for your scenario. Use HttpPost
instead and everything will work just fine.
In case you decide to use that method, then your form should use HttpDelete
to send the request to the server.
HttpDelete
is more suitable for a REST Api, I don't see you would get a benefit with that restriction in your case.
Upvotes: 2