【问题标题】:Bootstrap, foreach in view引导程序,视图中的 foreach
【发布时间】:2017-05-22 09:26:34
【问题描述】:

我有一个小问题。我店里有一些产品(我不知道会有多少产品)。在主站点中,我只想在一行中打印 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>

我的错误在哪里?我应该把这个循环放在另一个循环中吗?

【问题讨论】:

    标签: html twitter-bootstrap loops foreach row


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2016-06-11
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2016-04-24
      • 1970-01-01
      相关资源
      最近更新 更多