I'm trying to get into razor web pages and have this snippet of code which I can't work out how to get going successfully in razor. The for loop works correctly, but as soon as I add the if statement, it breaks.
@{
List<Plant> plants = PlantTableAdapter.Get().Where(t => t.featured).ToList();
for (int i = 0; i < plants.Count; i++) {
if (i % 4 == 0) {
<div class="row">
}
<div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
<div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
<div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
<div class="col-sm-3"><a href="#"><img class="img-responsive" src="../Style/Images/demo1.jpg" alt="variety name" /><p>Variety Name</p></a></div>
@*if ((i + 1) % 4 == 0) {
</div>
}*@
}
}
It seems no matter what combination I try, it just won't work. I came across this solution ASP.NET MVC Razor - if inside for for an if inside a for, but I think because the for is already inside a code block, it breaks it.
Help me stack overflow :)