How to handle an edit and delete button on the same form in ASP.NET MVC?
.net, asp.net, asp.net-mvc, c#
Solution
The best and easiest way would be to use two forms but don't nest them:
<h2>Edit SAS Program</h2>
@using (Html.BeginForm("Edit", "SasProgram", FormMethod.Post))
{
<label for="Name">Name</label>
@Html.TextBoxFor(model => model.Name)
<input type="submit" class="button" value="Save Changes" />
}
@using (Html.BeginForm("Delete", "SasProgram", FormMethod.Post))
{
<input type="submit" class="button" value="Delete" />
}
This way you have:
- Two separate forms
- No GET requests
- The delete button below the edit button, which makes more sense when you're on a view that allows you to edit something.
Problem
Consider the following markup: ``` <h2>Edit SAS Program</h2> @using (Html.BeginForm("Edit", "SasProgram", FormMethod.Post)) { <label for="Name">Name</label> @Html.TextBoxFor(model => model.Name) using (Html.BeginForm("Delete", "SasProgram", FormMethod.Post)) { <input type="submit" class="button" value="Delete" /> } <input type="submit" class="button" value="Save Changes" /> } ``` I'd like to have the `Delete` button on the same view as the `Edit`. However, it's not letting me have nested forms. What is the appropriate way to handle this situation? I tried leveraging this answer, How to handle nested forms in ASP.NET MVC, but it's a broken link now.