我试图在Razor视图中编写一个简单的循环,但到目前为止,正确的布局似乎已经提到了我。
我的代码如下:
@using RelensterV3.Helpers
@Html.BeginForm("SaveStockAnswers", "Call", FormMethod.Post, new { onsubmit = "return false;" }) {
<div class="ui-grid-c">
@{
int Count = 1;
string ClassToApply = "";
}
@foreach (var product in Model.StockAnswers)
{
if (Count == 1)
{
ClassToApply = "ui-block-a";
}
if (Count == 2)
{
ClassToApply = "ui-block-b";
}
if (Count == 3)
{
ClassToApply = "ui-block-c";
Count = 0;
}
Count++;
<div class="stock-product @ClassToApply">
@*<h2>@product.Product.Name</h2>*@
@Html.Partial("_StockAnswerForm", product)
</div>
}
</div>
}
但是,输出结果如下:
<form action="/Call/SaveStockAnswers" method="post" onsubmit="return false;">System.Web.Mvc.Html.MvcForm {
<div class="ui-grid-c">
<div class="stock-product ui-block-a">
<input type="hidden" name="stock.index" autocomplete="off" value="f8befb95-b30d-45bd-bc0e-6214d2001769" />
<div>.....etc etc
以尾随花括号结束。我不确定为什么要输出这些片段。
由于
答案 0 :(得分:5)
我会用:
@using (Html.BeginForm(etc..))
{
// Your loop code
}
答案 1 :(得分:4)
您忘记了html表单声明开头的using
语句。将其改写为
@using(Html.BeginForm ... etc, etc