简体   繁体   English

umbraco和C#Razor:如何从模型中的局部视图或页面视图中打印奇数和偶数vlaue替代项

[英]umbraco and c# Razor : how to print odd and even vlaue alternativelty in partialview or view in page from Model

需求

I have two html for left side image & content and for right side image & content. 我有两个用于左侧图像和内容以及右侧图像和内容的html。 value print from models. 从模型打印价值。 what can i write in for-each loop to print odd and even value alternatively in partial view in page from Model. 我可以在for-each循环中写些什么,以便在Model页面的部分视图中交替打印奇数和偶数。

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

I don't know much about umbraco, but why not try something simple like 我对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;
        }
    }

You can do like this 你可以这样

@{ int count = 1; @ {int count = 1;

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM