ASP.. NET MVC: Razor <不匹配结束标签
本文关键字:不匹配 结束 标签 Razor NET MVC ASP | 更新日期: 2023-09-27 18:16:22
我有一个充满列表模型的模型,我想使用foreach
循环来显示它,但是....我的HTML看起来像这样:
<tr>
<td class="lottery-unit lottery-unit-0"><img src="~/images/temp/1.png"></td>
<td class="lottery-unit lottery-unit-1"><img src="~/images/temp/2.png"></td>
<td class="lottery-unit lottery-unit-2"><img src="~/images/temp/4.png"></td>
<td class="lottery-unit lottery-unit-3"><img src="~/images/temp/3.png"></td>
</tr>
<tr>
<td class="lottery-unit lottery-unit-11"><img src="~/images/temp/7.png"></td>
<td colspan="2" rowspan="2">
<a href="#"></a>
</td>
<td class="lottery-unit lottery-unit-4"><img src="~/images/temp/5.png"></td>
</tr>
<tr>
<td class="lottery-unit lottery-unit-10"><img src="~/images/temp/1.png"></td>
<td class="lottery-unit lottery-unit-5"><img src="~/images/temp/6.png"></td>
</tr>
<tr>
<td class="lottery-unit lottery-unit-9"><img src="~/images/temp/3.png"></td>
<td class="lottery-unit lottery-unit-8"><img src="~/images/temp/6.png"></td>
<td class="lottery-unit lottery-unit-7"><img src="~/images/temp/8.png"></td>
<td class="lottery-unit lottery-unit-6"><img src="~/images/temp/7.png"></td>
</tr>
lottery-unit lottery-unit-0
到lottery-unit lottery-unit-11
是11我想要显示,tr
是不固定的。如果我用razor这样写:
foreach (var item in Model)
{
if (item.Order >= 0 && item.Order <= 3)
{
if (item.Order == 0)
{
<tr>
}
<td class="lottery-unit lottery-unit-0"><img src="~/images/temp/1.png"></td>
@if(item.Order == 3)
{
</tr>
}
}
以上将产生一些错误。那么如何编写正确的代码呢?
您需要在foreach
前面添加@
,并在if
语句中的<tr>
和</tr>
前面添加@:
。你的代码应该是这样的:
@foreach (var item in Model)
{
if (item.Order >= 0 && item.Order <= 3)
{
if (item.Order == 0)
{
@:<tr>
}
<td class="lottery-unit lottery-unit-0"><img src="~/images/temp/1.png"></td>
if (item.Order == 3)
{
@:</tr>
}
}
}