使用带有bootstrap网格的foreach列出项目

时间:2015-08-04 01:36:12

标签: jquery css twitter-bootstrap asp.net-mvc-5

我使用foreach列出产品列表,以使用此代码模拟商店的目录

@foreach (var item in Model){
       <div class="col-md-4">    
            <div class="wp-block product">
                 <figure>
                        @*<img alt="" src="~/Images/20852816-6959-f6580ba5 (1).jpg" class="img-responsive img-center">*@
                                            <img src="data:image/png;base64,@Convert.ToBase64String(item.Imagen, 0, item.Imagen.Length)" class="img-responsive img-center" />
                  </figure>
                  <h2 class="product-title"><a href="">@Html.DisplayFor(modelItem => item.Description)</a></h2>
                   <p>
                      Descripcion del producto
                  </p>
                  <div class="wp-block-footer">
                       <span class="price pull-left">@Html.DisplayFor(modelItem => item.Precio)</span>
                 <a href="#" class="btn btn-sm btn-base btn-icon btn-cart pull-right">
                       <span>Add to cart</span>
                  </a>
          </div>
     </div>    
</div>
}

但是有了这个,我只有3列3项,不管我有多少项,如果我有6项,我需要2列3列,但我不知道我有什么元素用css或jQuery修改。

2 个答案:

答案 0 :(得分:0)

您可以将内容包装在一行中,请参阅以下示例

 <div class="container" style="width: 800px">
   <div class="row">
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
     <div class="col-xs-4">.col-md-4</div>
  </div> 
</div>

https://jsfiddle.net/arc0g6ys/

<div class="container" style="width: your container with better to create a new class">
  <div class="row">
    @foreach (var item in Model){ 
       ...
    }
  </div>
</div>

答案 1 :(得分:0)

请尝试此输出。这将为每3个项目构建一个新行。

@{
    var itemCount = 0;
}

@{
    foreach (var item in Model.Reverse().Take(9))
    {
        var insertRow = itemCount % 3 == 0;

        if(insertRow)
        {
            @Html.Raw("<div class="row">
    ")
    }

                           <div class="col-md-4">
                               <div class="wp-block product">
                                   <figure>
                                       @*<img alt="" src="~/Images/20852816-6959-f6580ba5 (1).jpg" class="img-responsive img-center">*@
                                       <img src="data:image/png;base64,@Convert.ToBase64String(item.Imagen, 0, item.Imagen.Length)" class="img-responsive img-center" />
                                   </figure>
                                   <h2 class="product-title"><a href="">@Html.DisplayFor(modelItem => item.Description)</a></h2>
                                   <p>
                                       Descripcion del producto
                                   </p>
                                   <div class="wp-block-footer">
                                       <span class="price pull-left">@Html.DisplayFor(modelItem => item.Precio)</span>
                                       <a href="#" class="btn btn-sm btn-base btn-icon btn-cart pull-right">
                                           <span>Add to cart</span>
                                       </a>
                                   </div>
                               </div>
                           </div>

                               if (insertRow)
                               {
                               @Html.Raw("
                           </div>")
        }

        itemCount++;
    }
}