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

如何从@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的问题

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...