我认为有以下代码。在 for
语句内,我想将父 div
中的每 4 个项目分组。 If(counter == 0)
,我渲染父级 div
,然后渲染四个项目,然后如果它是 4 个项目组中的最后一个,我关闭父级div
。
但是,看起来 razor 希望我在第一个 if
中关闭父 div
。看来 Razor 不够聪明,无法知道父结束标签位于第二个标签内(如果下面有一些行,或者我可能遗漏了某些内容)。有什么想法可以解决这个问题吗?
@{int counter = 0; }
@for (int i = 0; i < Model.UserManagedRoles.Count; i++){
string roleName = Model.UserManagedRoles[i].Name.Replace(" ", "").Trim();
string id = string.Format("chk{0}", roleName);
if (counter == 0)
{
<div class="rowCapture divUserRoles">
}
<div class="hzStackSmall">
<input type="checkbox" id="@id" name="@id" value="@roleName" />@Model.UserManagedRoles[i].Name</div>
if(counter == 3 || i == Model.UserManagedRoles.Count) {
</div>
}
counter++;
if (counter == 4)
{
counter = 0;
}
}
最佳答案
您必须使用@: <div class="hzStackSmall">
使用 Razor 引擎打印 HTML 代码。这会有帮助吗?
我强烈推荐this来自 ScottGu 博客的关于 Razor 引擎的教程。
@{int counter = 0; }
@for (int i = 0; i < Model.UserManagedRoles.Count; i++)
{
string roleName = Model.UserManagedRoles[i].Name.Replace(" ", "").Trim();
string id = string.Format("chk{0}", roleName);
if (counter == 0)
{
@:<div class="rowCapture divUserRoles">
}
@:<div class="hzStackSmall">
@:<input type="checkbox" id="@id" name="@id" value="@roleName" />@Model.UserManagedRoles[i].Name</div>
if(counter == 3 || i == Model.UserManagedRoles.Count) {
@:</div>
}
counter++;
if (counter == 4)
{
counter = 0;
}
}
关于asp.net-mvc - Razor .NET 无法识别代码脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9386917/