At first use I loved it and it felt so much less cluttered then the webforms <%: %> etc. view engine,
but upon using it further I can’t help but notice it’s hypersensitive to where its “{” parentheses are placed and other scenarios. It gives errors at points where the older view engine was not as picky.
For example the below code will produce an error because the form helper closing bracket }
is under the </table> tag. If I place it higher above the </tbody> it works! But I don’t need
it there because the submit button input has to be nested within and I don’t want to put the button input into the table.
@model CartTest.Models.Cart
@{
ViewBag.Title = "Index";
}
<h2>Cart Index</h2>
<table width="80%" align="center">
<thead>
<tr>
<th align="center">Quantity</th>
<th align="left">Item</th>
<th align="right">Price</th>
<th align="right">Subtotal</th>
</tr>
</thead>
<tbody>
@{int index = 0;}
@using (Html.BeginForm("UpdateCart","Cart"))
{
foreach (var line in Model.Lines)
{
<tr>
@Html.Hidden("Lines.Index", index)
<td align="center">@Html.TextBox("Lines[" + index + "].Quantity", line.Quantity)</td>
<td align="left">@line.Product.Name</td>
<td align="right">@line.Product.Price</td>
<td align="right">@(line.Quantity * line.Product.Price)</td>
<td align="right">@Html.ActionLink("Remove", "RemoveItem", new { productId = line.Product.ProductID }, null)</td>
</tr>
index++;
}
</tbody>
<tfoot></tfoot>
</table>
<input type="submit" value="Update Cart" />
}
The reason it works above the
</tbody>is because you declared the BeginForm inside the opening<tbody>. They must be nested properly in order to work. If you don’t want to put the input button inside the table, then move the BeginForm outside the table element so the opening and closing brace are at the same level.