Given the following Razor code:
<tbody>
@foreach (Profession profession in Model)
{
<tr>
<td>@profession.Name</td>
<td>@profession.PluralName</td>
<td>@Html.ActionLink("Edit", "AddOrEdit", new { Id = profession.ProfessionID })</td>
</tr>
}
</tbody>
What's the simplest way to provide some kind of alternate row styling? (i.e. different styling for odd and even rows.)
I don't seem to be able to add arbitrary C# to declare a bool
variable which gets flipped each iteration of the foreach
loop in order to set a classname for the tr
, for example.
JQuery can do that in the client side (and I would probably use client side scripting here rather than server logic).
$("tr:odd").css("background-color", "#bbbbff");
You can also use just a simple variable to set the css class (almost pseudo-code):
@foreach (Profession profession in Model)
{
@i++;
<td class="@i%2==0?"even":"odd"">
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With