We're building an application in MVC3, and we want to have editable data tables. What we're after is something like this (slightly abridged):
<table class="project-items data-grid">
foreach (var item in Model.Items) {
using (Html.BeginForm("SaveItem", "Project", FormMethod.Post)) {
<tr>
<td>@Html.TextBox("Title", item.Title) @Html.Hidden("ProjectID", proj.ProjectID) @Html.Hidden("ItemID", item.ItemID)</td>
<td>@Html.DropDownList("State", item.State.ToSelectList(), "")</td>
<td>@Html.DropDownList("QuoteID", Model.Quotes.ToSelectList(item.QuoteID), "")</td>
<td>@Html.TextBox("EstimatedCost", item.EstimatedCost, new { @class = "costfield" })</td>
<td>@Html.TextBox("VendorEstimatedCost", item.VendorEstimatedCost, new { @class = "costfield" })</td>
<td>@Html.TextBox("VendorQuotedCost", item.VendorQuotedCost, new { @class = "costfield" })</td>
<td><input type="submit" value="Save" name="Save"/></td>
</tr>
}
}
</table>
Of course, putting a <form>
directly inside a <table>
isn't valid HTML; it seems to work in some cases, but in others, it totally freaks out the browser and/or model binder.
What's the safest, cleanest way to do this so that we don't rely on undefined behaviors? I'd like to have something suitable for forms with or without AJAX. I played around with using <div>
and display: table-cell
tricks, but that prevents the use of colspan
, which we also need to do.