Reputation: 5266
I just caught myself doing this:
<table>
<tr>
<th>Code</th>
<th>Enabled</th>
<th>Percentage</th>
<th>Amount</th>
<th>Start</th>
<th>End</th>
<th>Action</th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>@Html.ActionLink(item.Code, "Edit", new { id = item.Id })</td>
<td>@item.Enabled ? "Yes" : "No"</td>
<td>@item.Percentage</td>
<td>@item.Amount</td>
<td>@item.StartDate</td>
<td>@item.EndDate</td>
<td>
@using (Html.BeginForm("Delete", "Discounts", new { id = item.Id }))
{
<input type="submit" value="Delete" />
}
</td>
</tr>
}
</table>
The reason I stopped was because I have a form in every row, as a good practice would it be better to have the entire table wrapped in a single form instead?
I probably already know the answer but thought I'd check :-)
Upvotes: 6
Views: 3722
Reputation: 3719
I think this is the best approach, unless you want to implement a mass delete feature (your HTML code tells me that this is not the case).
By using a form (with POST method) for each item you make sure that
http://[yoursite]/[yourapp]/[delete_method]/[item_id_to_delete]
. So, go with your current approach. (my 0.02 EUR)
Upvotes: 0
Reputation: 14941
I think your approach is fine and is often the best way. e.g. if you were writing an app with a voting system like stackoverflow (example chosen because you are looking at such an app right now) and you wanted to implement the voting mechanism by using HttpPost
then you might create a small control with the up and down buttons as separate forms*. That way you could easily add any number of such "widgets" to a page and the containing page wouldn't need to know anything about them (including if they were even present). Indeed, you could use unobtrusive javascript to submit the form and reload the vote "widget" with a failback to reload the page if javascript was turned off and it could all look pretty nice.
*NOTE: I don't think that's how they do it at stackoverflow, mind you!
Non-AJAX failback could be like this:
[HttpPost]
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public ActionResult DoThing(whatever params you are passing in)
{
// Do stuff and then...
if (Request.IsAjaxRequest)
{
return View("_partialThingView", thingViewModel);
}
else
{
RedirectToAction("Index");
}
}
Upvotes: 2
Reputation: 8380
I would say it depends on your particular scenario.
Based on my current knowledge of your situation I would probably go with what you have now, just because it is nice and clean.
Upvotes: 6