【问题标题】:How to add div tag after fixed number of iteration in mvc如何在mvc中固定迭代次数后添加div标签
【发布时间】:2017-11-23 07:53:21
【问题描述】:

我正在使用 Umbraco (mvc) 和 bootstrap。我的模板使用局部视图在模板中加载员工的姓名和职位。如何在 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>
          }

难度:

我要添加

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

每 4 次之后

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

【问题讨论】:

    标签: asp.net-mvc twitter-bootstrap-3 umbraco7


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试:

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

      【讨论】:

        【解决方案3】:
         -  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>
                   }
        

        【讨论】:

          猜你喜欢
          • 2012-04-17
          • 1970-01-01
          • 1970-01-01
          • 2019-08-07
          • 2013-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多