umbraco和c#Razor:如何在部分视图中打印奇数和偶数的替代品或从模型中查看页面中的视图

时间:2017-12-27 05:24:49

标签: c# razor umbraco

Requirement

我有两个html用于左侧图像&内容和右侧图像&内容。从模型打印的价值。我可以写什么 - 每个循环在模型页面的局部视图中交替打印奇数和偶数值。



    @inherits UmbracoViewPage<List<ProjectName.Models.DealerSectionModel>>
    @using ProjectName.Models;
    @using Umbraco.Core;
    
    
    @{ 
        foreach (DealerSectionModel historylist in Model)  // what should i write in this loop .(ex if 1 value in model then print odd html)
        {
            if ()
            {
                @RenderEvenHistoryList(historylist)
            }
            else
            {
                @RenderOddHistoryList(historylist)
            }
           
        }
    }
    
    @helper RenderOddHistoryList(DealerSectionModel item)
            {
            <div class="row history-second-section py-2 py-xl-4 py-lg-4 py-md-4 py-sm-2">
                <div class="col-12 col-xl-6 col-lg-6 col-md-6 col-sm-12 history-second-images">
                    <div class="quote my-3"><iframe src="@item.VideoUrl" width="510" height="282" frameborder="0" allowfullscreen></iframe></div>
                </div>
                <div class="col-12 col-xl-6 col-lg-6 col-md-6 col-sm-12 history-second-content">
                    <div class="content-year">@item.Title </div>
                    <h4>@item.ImageTitle</h4>
                    <p>@item.ImageDescription</p>
                    
                </div>
            </div>
    
    }
    
    }
    
    @helper RenderEvenHistoryList(DealerSectionModel item)
            {
       // render html for even model value
    }
    
    }
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

我对umbraco不太了解,但为什么不尝试像

这样简单的事情
 @{ 
     var even = false;
        foreach (DealerSectionModel historylist in Model)  // what should i write in this loop .(ex if 1 value in model then print odd html)
        {
            if (even)
            {
                @RenderEvenHistoryList(historylist)
            }
            else
            {
                @RenderOddHistoryList(historylist)
            }
           even = !even;
        }
    }

答案 1 :(得分:1)

你可以这样做

@ {int count = 1;

foreach (DealerSectionModel historylist in Model)
{
    if (count % 2 == 0)
    {
        @RenderEvenHistoryList(historylist)
    }
    else
    {
        @RenderOddHistoryList(historylist)
    }
    count++;
}

}