我的foreach循环中有很多记录,我想只显示两条记录

时间:2015-04-28 06:09:54

标签: entity-framework model-view-controller

 @foreach (var item in Model)
                   { 
                      <div class="row leftimages">
                             <div class="col-md-6 col-xs-12 col-sm-12 innerimage">
                           <a href="@Html.Raw(Html.Encode(@Html.DisplayFor(modelItem => item.DealUrl)).Replace("&amp;", "&"))" target="_blank"><img src="@Html.DisplayFor(modelItem => item.ImageUrl)" class="img-responsive1"> </a>
                             </div>
                             <div class="col-md-6 col-xs-12 col-sm-12">
                                <h4 class="tag">@Html.DisplayFor(modelItem => item.Title)</h4>
                                  <h5 class="greentext">@Html.DisplayFor(modelItem => item.VendorName)</h5>
                                  <h5 class="freeship"></h5>
                                @{ if (item.OfferPrice.HasValue)
                                   { 
                                       <h5 class="freeship">Discount Available of @Html.DisplayFor(modelItem => item.Discount)%</h5>
                                  <h4 class="price">  @Html.DisplayFor(modelItem => item.OfferPrice)  <span class="oldprice" style="text-decoration: line-through;"> @Html.DisplayFor(modelItem => item.OriginalPrice)</span></h4>
                                   }
                                 }
                                    </div>
                             <div class="col-md-12">
                              <p class="paragraph"> @Html.DisplayFor(modelItem => item.ProductDescription)</p>                  
                             </div>
                       </div>
                       <div class="row blackstrip">
                           <div class="col-md-2 col-xs-3">
                               <h5 class="lefticons"><img src="images/like.png" ><a href="#">Like</a></h5>
                           </div>
                           <div class="col-md-2 col-xs-3 icon">
                               <h5 class="lefticons"><img src="images/share.png"><a href="#">Share</a></h5>
                           </div>
                           <div class="col-md-2 col-xs-3 icon">
                               <h5 class="lefticons"><img src="images/more.png"><a href="#">More</a></h5>
                           </div>
                           <div class="col-md-3 col-xs-3 col-md-offset-3 buy">
                             <h4 class="buynow"><a href="@Html.Raw(Html.Encode(@Html.DisplayFor(modelItem => item.DealUrl)).Replace("&amp;", "&"))" target="_blank">Buy Now</a></h4>
                               </div>
                       </div>
                    }

1 个答案:

答案 0 :(得分:0)

您可以使用LINQ的全部功能

@foreach (var item in Model.Take(2)) 

更好的是,您可能希望限制从控制器中的数据库返回的数据量。

喜欢db.YourDbSet.Where(x=> some condition).Take(2).ToList()