ASP.NET MVC 2 renders a link (i.e. <a>) to delete records.
It can be harmful to allow delete actions through GET actions, so I want to do the delete by issuing a POST.
I’ve created the following piece of code:
<% using (Html.BeginForm("Delete", "Boodschap", new { id = item.BoodschapID }))
{ %>
<button>Delete</button>
<% } %>
Now I would like to add this code to the Html helper as an extension method:
public static MvcForm DeleteButton(this HtmlHelper helper, string name,
string actionName, string controllerName, string routeValues)
{
MvcForm form = helper.BeginForm(actionName, controllerName, routeValues);
return form;
}
Now here is where I got stuck. How do I get this delete button to work?
If you want to generate the full code, you’re going about it wrong to have it return an
MvcForm. You want to have it return anMvcHtmlStringand construct the HTML within the method. That way you can use it as:Generating the HTML directly (note: untested, you may need suitable null checks, etc.)
An alternative would be to reuse the form helpers and Response.Write, but have the method return an (empty) string, perhaps something like: