嘿我只需要在div高于6时才将div添加到div中,但我不知道问题出在哪里。感谢您的任何建议
@{
int count = 0;
}
@foreach (var item in Model.Models)
{
<div class="col-xxs-12 col-xs-6 col-sm-4 @(count < 6 ? "" : "dismiss")">
<a href="@Url.Action("Detail", "Product", new { productID = item.ProductID, parentCategoryID = item.CategoryID, isProductModel = true })" class="model-wrap">
<div class="box">
<div class="img-wrap" style="background-image: url( @Url.Content(Utils.ProductImagePath(item.ImageID)) )"></div>
<div class="arrow f16">@item.Name <i class="fa fa-angle-right theme-color" aria-hidden="true"></i></div>
</div>
</a>
</div>
} count++;
</div>
答案 0 :(得分:2)
你的问题是“count ++”不在foreach循环中。 如果你想使用foreach循环,你应该在循环内移动“count ++”。
@{
int count = 0;
}
@foreach (var item in Model.Models)
{
<div class="col-xxs-12 col-xs-6 col-sm-4 @(count < 6 ? "" : "dismiss")">
<a href="@Url.Action("Detail", "Product", new { productID = item.ProductID, parentCategoryID = item.CategoryID, isProductModel = true })" class="model-wrap">
<div class="box">
<div class="img-wrap" style="background-image: url( @Url.Content(Utils.ProductImagePath(item.ImageID)) )"></div>
<div class="arrow f16">@item.Name <i class="fa fa-angle-right theme-color" aria-hidden="true"></i></div>
</div>
</a>
</div>
count++;
}
答案 1 :(得分:0)
关于设置课程
您现有的代码看起来很好,因为如果您的计数值大于或等于6,则只会通过您的三元语句添加解雇类:
<div class="col-xxs-12 col-xs-6 col-sm-4 @(count < 6 ? "" : "dismiss")">
确定您的迭代范围
您是否考虑使用for
循环,它将自动处理为您存储当前索引?这可能会使计数更容易跟踪并解决计数器未被正确定位的任何可能问题(因为count++
不在foreach
循环中):
@for (var i = 0; i < Model.Models.Count(); i++)
{
// Store a reference to your item to ease syntax
var item = Model.Models.ElementAt(i);
<div class="col-xxs-12 col-xs-6 col-sm-4 @(i < 6 ? "" : "dismiss")">
<a href="@Url.Action("Detail", "Product", new { productID = item.ProductID, parentCategoryID = item.CategoryID, isProductModel = true })" class="model-wrap">
<div class="box">
<div class="img-wrap" style="background-image: url( @Url.Content(Utils.ProductImagePath(item.ImageID)) )"></div>
<div class="arrow f16">@item.Name <i class="fa fa-angle-right theme-color" aria-hidden="true"></i></div>
</div>
</a>
</div>
}