I'm using bootstrap and showing a grid of items. I show 6 items a row.
Currently I'm having this code below, the code works but the smell of code is bad.
@{ int rowcnt = 0; }
@for (int i = 0; i < Model.Count; i++)
{
if (rowcnt % 6 == 0)
{
@Html.Raw("<div class=\"row\">")
}
<div class="col-md-2 col-xs-6 col-sm-6">
<div class="myItemContainer">
<!-- fancy item stuff -->
</div>
</div>
rowcnt++;
if (rowcnt % 6 == 0)
{
@Html.Raw("</div>")
}
}
@*for-loop end*@
@if (rowcnt % 6 != 0)
{
@Html.Raw("</div>")
}
Is there a better way to do this with bootstrap without having to count the rows to insert div's?
I tested to just create col- elements but that creates strange rows with only a single item sometimes in a row.
item item item item
item
item item item item
item
item item item item
item
...
Html.Rawhelper. That's the beauty of Razor - you can mix the C# and HTML syntax up.<div class"row">@for (int i = 0; i < Model.Count; i++){if (i > 0 && i % 6 == 0){@:</div><div class"row">}<div class="col-md-2 col-xs-6 col-sm-6">.....</div></div>