【发布时间】:2016-08-12 08:13:31
【问题描述】:
我的 Index.cshtml(视图)代码中有这个:
<div class="col-md-6">
<label class="control-label">Delivery Performance (report spans a number of days)</label>
<br>
<label>From</label>
<select>
@for (int i = 1; i <= @maxDaysBackForDeliveryPerformance; i++)
{
<option id="selItem_@(i) value=@"i">@i</option>
}
</select>
<label>days back</label>
<br>
<label>To</label>
<select>
@for (int j = 1; j <= @maxDaysBackForDeliveryPerformance; j++)
{
<option id="selItem_@(j) value=@"j">@j</option>
}
</select>
<label>days back</label>
<br>
<button id="btnTestDeliveryPerformanceSettings">Test Settings</button>
</div>
这运行良好(在添加底部部分之前):
<div class="col-md-6">
<label class="control-label">Delivery Performance (report spans a number of days)</label>
<br>
<label>From</label>
<select>
@for (int i = 1; i <= @maxDaysBackForDeliveryPerformance; i++)
{
<option id="selItem_@(i) value=@"i">@i</option>
}
</select>
<label>days back</label>
</div>
...但是当我添加基本相同的代码时:
<br>
<label>To</label>
<select>
@for (int j = 1; j <= @maxDaysBackForDeliveryPerformance; j++)
{
<option id="selItem_@(j) value=@"j">@j</option>
}
</select>
<label>days back</label>
<br>
<button id="btnTestDeliveryPerformanceSettings">Test Settings</button>
...它因解析器错误而失败。我在它看到的代码块的开头没有看到“j”?
这是它所暗示的 (193) 行:
<option id="selItem_@(j) value=@"j">@j</option>
什么出了问题?
更新
像这样在引号内移动@符号:
<option id="selItem_@(j) value="@j">@j</option>
...将 err msg 上移两行;现在它在抱怨这条线:
@for (int j = 1; j <= @maxDaysBackForDeliveryPerformance; j++)
...也就是说,“for 块缺少一个结束“}”字符。确保该块中的所有“{”字符都有一个匹配的“}”字符,并且没有一个"}" 字符被解释为标记。"
【问题讨论】:
-
应该是
id="selItem_@(j)" value="@j" -
那为什么“i”的那个起作用了?
-
它没有 - 您的引号不匹配,并且还需要更正。 (但是你为什么不使用
DropDownListFor()方法来生成你的选择呢?) -
自从我使用这项技术已经有好几年了;感谢“DropDownListFor()”的提醒。
标签: html asp.net-mvc razor view