Asp.net mvc 如何在mvc中固定迭代次数后添加div标记

Asp.net mvc 如何在mvc中固定迭代次数后添加div标记,asp.net-mvc,twitter-bootstrap-3,umbraco7,Asp.net Mvc,Twitter Bootstrap 3,Umbraco7,我正在使用带引导的Umbraco(mvc)。我的模板使用局部视图在模板中加载员工的姓名和职务。如何在4次迭代后添加div(新行) @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ var selection = Model.Content.Site().FirstChild("aboutUs").Children("staff") .Where(x => x.IsVisible());

我正在使用带引导的Umbraco(mvc)。我的模板使用局部视图在模板中加载员工的姓名和职务。如何在4次迭代后添加div(新行)

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{
    var selection = Model.Content.Site().FirstChild("aboutUs").Children("staff")
                        .Where(x => x.IsVisible());
}
    @foreach(var item in selection){
          <div class="row team">
                <div class="col-sm-3">
                    <div class="thumbnail-style">
                        <h3><a>@item.GetPropertyValue("fullName")</a> <small>@item.GetPropertyValue("jobTitle")</small></h3>
                    </div>
                </div>
          </div>
          }
@继承Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var selection=Model.Content.Site().FirstChild(“aboutUs”).Children(“staff”)
。其中(x=>x.IsVisible());
}
@foreach(选择中的var项目){
@item.GetPropertyValue(“全名”)@item.GetPropertyValue(“职务头衔”)
}
难度:

我想补充一点

 <div class="row team">
 </div>

每四点以后

<div class="col-sm-3">
</div>

-它会工作,使用for循环而不是foreach循环,然后只添加逻辑
@for(int i=1;i
试试:

@foreach(选择中的var obj.Select((项,索引)=>new{item,index}))
{
如果(对象索引==0 | |对象索引%4==0){
@Html.Raw(“”)
}
@obj.item.GetPropertyValue(“全名”)
@obj.item.GetPropertyValue(“jobTitle”)
@如果((对象索引+1)%4==0 | |对象索引+1==selection.Count()){
@Html.Raw(“”)
}
}

更简单的解决方案是使用Umbraco helper方法
InGroupsOf(columns)

@foreach(选择中的变量组。InGroupsOf(4))
{
@foreach(组中的var项目)
{
@item.GetPropertyValue(“全名”)@item.GetPropertyValue(“职务头衔”)
}
}
 -  It will works, Use for loop instead of foreach loop and then just add logic

        @for (int i = 1; i < selection.Count; i++)
        {
        var item = selection[i];
        <div class="row team">
            <div class="col-sm-3">
               <div class="thumbnail-style">
                @if (i % 5 == 0)
                {
                    <div class="col-sm-3">
                        <h4 >D @item</h4> <!--your actual html code-->
                    </div>
                }
                else
                {
                    <h4 style="color:red">D @item</h4> <!--your actual html code-->
                }
            </div>
        </div>
    </div>
           }
 @foreach(var obj in selection.Select((item, index) => new {item, index}))
 {
          if(obj.index==0||obj.index%4==0){
              @Html.Raw("<div class='row team'>")
          }
                <div class="col-sm-3">
                    <div class="thumbnail-style">
                        <h3><a>@obj.item.GetPropertyValue("fullName")</a> <small>
                            @obj.item.GetPropertyValue("jobTitle")</small></h3>
                    </div>
                </div>
           @if((obj.index+1)%4==0||obj.index+1==selection.Count()){
           @Html.Raw("</div>")
          }
}
@foreach(var group in selection.InGroupsOf(4))
{
   <div class="row team">
      @foreach(var item in group)
      {
         <div class="col-sm-3">
             <div class="thumbnail-style">
                 <h3><a>@item.GetPropertyValue("fullName")</a> <small>@item.GetPropertyValue("jobTitle")</small></h3>
             </div>
         </div>
      }
   </div>
}