如何添加HTML页面剃刀内if语句在foreach? [英] How to add html to the page inside a Razor if statement in a foreach?

查看:90
本文介绍了如何添加HTML页面剃刀内if语句在foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能在 @foreach添加HTML页面从如果()()内。我得到不得到任何特定的错误,它只是不采取第二个如果()。它写出来,比如文本。

这是我试过,但没有运气

  @foreach(以型号VAR项)
    {
        如果(计数%4 == 0 || totaltCount == 1)
        {
        < D​​IV CLASS =在教官容器>
            }
            < D​​IV CLASS =在教官>
                < H3> @ item.Name< / H3 GT&;
                @ item.Information
            < / DIV>
        如果((计数 - 1)%3 == 0 ||计数== totaltCount){
        < / DIV>
        }        算上++;
    }

我的html得到这样

 < D​​IV CLASS =在教官容器>
            }
            < D​​IV CLASS =在教官>                < H3>被测者0℃; / H3 GT&;
                唧唧歪歪喇嘛唧唧歪歪
            < / DIV>
        如果((计数 - 1)%3 == 0 ||计数== totaltCount){
        < / DIV>


解决方案

您需要添加@:在html的前盖内的if语句:

  @foreach(以型号VAR项)
{
    如果(计数%4 == 0 || totaltCount == 1)
    {
        @:其中,DIV CLASS =在教官容器>
    }
        < D​​IV CLASS =在教官>
            < H3> @ item.Name< / H3 GT&;
            @ item.Information
        < / DIV>    如果((计数 - 1)%3 == 0 ||计数== totaltCount){
        @:LT; / DIV>
    }    算上++;
}

我发现从这个解决方案这个计算器的问题

How can I add html to the page from inside an if() in a @foreach(). I get dont get any specific error it just does not take the second if(). It writes it out like text.

this is what i tried but with no luck

@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++;
    }

my html get like this

        <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>

解决方案

You need to add @: in front of the html inside the if statements:

@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++;
}

I found this solution from this stackoverflow question

这篇关于如何添加HTML页面剃刀内if语句在foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆