gpt4 book ai didi

html - 无法使用 Razor 在 foreach 中关闭标记 - ASP.NET MVC

转载 作者:行者123 更新时间:2023-11-28 05:23:28 24 4
gpt4 key购买 nike

我有一个 div,每个 div 都有两个 div,如下所示:

<div class="col">
<div class="item-c color1">..</div>
<div class="item-c color2">..</div>
</div>

我从数据库中读取数据,我想用 foreach 语句重复这个 div。

我使用下面的代码,但出现了运行时错误

<div class="col">
@foreach (var item in Model)
{

<div class="item-c @("color" + index.ToString()) ">..</div>
if (index % 2 == 0)
{
<text></div><div class="col"></text>
}
index++;
if (index > 8) { index = 1; }
}

我的结果应该是这样的: enter image description here我收到此错误:

Encountered end tag "div" with no matching start tag

最佳答案

你的代码需要

@{ int index = 1; }

<div class="col">
@foreach(var item in Model.Items)
{
<div class="item-c color@(index)">..</div>
if (index % 2 == 0) // close the div and start a new one
{
@:</div><div class="col">
}
index++;
if (index > 8) { index = 1; }
}
</div>

关于html - 无法使用 Razor 在 foreach 中关闭标记 - ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35540279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com