How do I create a new table row on every 4th loop in my Razor View? This is creating a new row for each number before 4, and then quits creating new rows:
@{
int i = 0;
}
@foreach (var item in ViewBag.ProgramIdList)
{
if((i / 4) == 0)
{
@:<tr>
}
<td>
<input type="checkbox" name="@item.ProgramId" id="@item.ProgramId" />
<label for="@item.ProgramTitle">@item.ProgramTitle</label>
</td>
if((i / 4) == 0)
{
@:</tr>
}
i++;
}
Use the modulo operator. For :
and
If the number of items doesn’t divide into even rows, you would add the remaining cells and a closing row tag after the loop: