加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 如何添加html到页面里面的一个Razor if语句在f

发布时间:2020-12-16 00:27:59 所属栏目:asp.Net 来源:网络整理
导读:如何从@foreach()中的if()里面的页面添加html。我没有得到任何具体的错误,它不会采取第二个if()。它像文本一样写出来。 这是我试过但没有运气 @foreach (var item in Model) { if (count % 4 == 0 || totaltCount == 1) { div class="in-instructor-contain
如何从@foreach()中的if()里面的页面添加html。我没有得到任何具体的错误,它不会采取第二个if()。它像文本一样写出来。

这是我试过但没有运气

@foreach (var item in Model)
    {
        if (count % 4 == 0 || totaltCount == 1)
        {
        <div class="in-instructor-container">
            }
            <div class="in-instructor">
                <h3>@item.Name</h3>
                @item.Information
            </div>
        if ((count - 1) % 3 == 0 || count == totaltCount) {
        </div>
        }

        count++;
    }

我的html就像这样

<div class="in-instructor-container">
            }
            <div class="in-instructor">

                <h3>Test Person 0</h3>
                bla bla bla bla bla bla bla
            </div>
        if ((count - 1) % 3 == 0 || count == totaltCount) {
        </div>

解决方法

你需要添加@:在if语句之前的html里面:
@foreach (var item in Model)
{
    if (count % 4 == 0 || totaltCount == 1)
    {
        @:<div class="in-instructor-container">
    }
        <div class="in-instructor">
            <h3>@item.Name</h3>
            @item.Information
        </div>

    if ((count - 1) % 3 == 0 || count == totaltCount) {
        @:</div>
    }

    count++;
}

我发现这个解决方案从this stackoverflow的问题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读