我现在正在编写一些非常糟糕的代码,在保存之前,我希望得到一些关于改进它的意见。我正在尝试构建一个每行三个单元格的 html 表格。如果集合有 5 个项目,则应呈现为两行。
到目前为止我所写的代码不是很健壮,需要不断维护,但我不确定是否有其他工具/方法来完成任务。
<table>
@foreach (VideosModel item in Model)
{
if (cellCount == 0 || cellCount == 3)
{
@Html.Raw("<tr>")
}
<td style="padding:0px 20px;">
@item.VideoTitle <br />
<a href="@item.VideoUrl" target="_blank">
<img src="../../.../Video.jpg" /> <br />
</a>
@item.VideoDescription
<br />
</td>
cellCount++;
if (cellCount == 3 || cellCount == 6)
{
@Html.Raw("</tr>")
}
if (cellCount > 3) { cellCount = 0; }
}
</table>
您应该考虑使用模数,而不是将 cellCount 的值与 0、3 或 6 进行比较:
<table>
@foreach (VideosModel item in Model)
{
if (cellCount % 3 == 0)
{
@:<tr>
}
<td style="padding:0px 20px;">
@item.VideoTitle <br />
<a href="@item.VideoUrl" target="_blank">
<img src="../../.../Video.jpg" />
</a><br />
@item.VideoDescription
<br />
</td>
cellCount++;
if (cellCount % 3 == 0)
{
@:</tr>
}
}
</table>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)