Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc - Simplest way to provide alternate row styling in Razor

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.

like image 440
David Avatar asked Oct 28 '11 09:10

David


1 Answers

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""> 

}
like image 174
Aliostad Avatar answered Sep 18 '22 16:09

Aliostad