below code working perfectly . but i need to convert it from foreach
to for
loop .please help me to iteration by for loop. because i have to introduce some if
else
statement in entire code
@foreach (var item in ViewData["productSl_1"] as IEnumerable<Nazmulkp.Models.Product>)
{
<div class="col-md-3 ">
<div class="thumbnail">
<a href="#" class=""><img src="@Url.Content(item.ImagePath)" alt="Image" style="max-width:100%;"></a>
<div class="caption">
<h5>@item.ProductName</h5>
<h4>Tk.@item.Price</h4>
</div>
</div>
</div>
}
Example:
I have to make sure if index number 2 than execute <div class="col-md-3>"
Thank you
You do not need to convert your foreach
to a for loop for adding a conditional css class. Simply add a counter variable to your code and use that for your if condition as needed.
For example, the below code will add the css class "col-md3"
to the 3rd item in your collection.
@{ var counter = 0;}
@foreach (var item in YouCollectionHere)
{
<div class="@(counter==2?"col-md-3":"")">
<h2>@item.Name</h2>
</div>
counter++;
}