Assuming you would rather not use CSS (i.e. :nth-child(odd)
) you can either:
-
use a normal for loop:
@for (int i = 0; i < Model.Count; i++) { ... }
-
use
.Select
:@foreach (var item in Model.Select((x, i) => new { Data = x, Index = i })) { ... }
Either way, you’d have access to the current index and could then use i % 2 == 1
as the condition for your background-color. Something like:
<tr style="background-color:@(i % 2 == 1 ? "red" : "white")">...</tr>