Bootstrap,foreach在视野中

时间:2017-05-22 09:26:34

标签: html twitter-bootstrap loops foreach row

我有点问题。我店里有一些产品(我不知道会有多少产品)。在主站点,我想在一行中只打印3个产品。而且我不知道如何循环来完成这项工作。现在在我的代码中我只有一行而且看起来并不好看。我设计的行t只包含3个产品。

 <div class="row-fluid">
                <ul class="thumbnails">
                    @if (count($subcategoryProducts)==0)
                        There's no products
                    @else

                        @foreach($subcategoryProducts as $product)
                            <li class="span4">
                                <div class="thumbnail">
                                    <a href="/product/{{$product->id}}" class="overlay"></a>
                                    <a class="zoomTool" href="/product/{{$product->id}}" title="add to cart"><span
                                                class="icon-search"></span> Details</a>
                                    <a href="/product/{{$product->id}}"><img src="{{$product->getImageURL()}}"
                                                                            alt=""></a>

                                    <div class="caption cntr">
                                        <p>{{$product->name}}</p>

                                        <p><strong> {{$product->price}}</strong></p>
                                        <h4><a class="shopBtn" href="#/{{$product->id}}" title="add to cart"> Add to cart </a>
                                        </h4>
                                        <br class="clr">
                                    </div>
                                </div>
                            </li>

                        @endforeach

                    @endif
                </ul>
  </div>

我的错误在哪里? Shoul我把这个循环放在另一个可能吗?

1 个答案:

答案 0 :(得分:0)

JERRIE PELSER在这里的方法2是我今天祷告的答案。

使用简单的扩展方法

public static class ArrayExtensions
{
    /// <summary>
    /// Splits an array into several smaller arrays.
    /// </summary>
    /// <typeparam name="T">The type of the array.</typeparam>
    /// <param name="array">The array to split.</param>
    /// <param name="size">The size of the smaller arrays.</param>
    /// <returns>An array containing smaller arrays.</returns>
    public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size)
    {
        for (var i = 0; i < (float)array.Length / size; i++)
        {
            yield return array.Skip(i * size).Take(size);
        }
    }
}

使用foreach中的扩展方法将行拆分所需的列数

@model IEnumerable<BootstrapGridLayout.Models.Article>
@using BootstrapGridLayout;
@{
    ViewBag.Title = "Home Page";
}

@foreach (var row in Model.ToArray().Split(3))
{
    <div class="row">
        @foreach (var article in row)
        {
            <div class="col-md-4">
                <div class="thumbnail">
                    <img src="@article.Thumbnail" data-holder-rendered="true" style="height: 200px; width: 100%; display: block;">
                    <div class="caption">
                        <h3 id="thumbnail-label">@article.Name</h3>
                        <p>@article.Description</p>
                    </div>
                </div>
            </div>
        }
    </div>
}