Search code examples
razorumbracorazor-2umbraco7umbraco6

how to give alternating class to alternating item of @foreach look in Razor + umbraco


I am using Umbraco 7.x

I need some thing like following In list Item generated using for each. alternating item need to give respective class.

enter image description here

So is their any was to determine even and odd row to give respective class name.

As following is my code for the same.

@foreach (var itemTblRows in @Model.Children) 
        {       
         <tr class="light">
                <td>@itemTblRows.ABL_tableData1</td>
                <td>@itemTblRows.ABL_tableData2</td>
                <td>@itemTblRows.ABL_tableData3</td>
                <td>@itemTblRows.ABL_tableData4</td>
              </tr>
        }

I will not like to use CSS and JS to do so, because in other case (with diff. lay out) it will not work.


Solution

  • You can do easily with the IsOdd and IsEven helper methods:

    <tr class="@itemTblRows.IsOdd("odd","even")>
    

    or

    <tr class="@itemTblRows.IsEven("even","odd")>