Results 1 to 2 of 2

Thread: [RESOLVED] alternating a class

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Resolved [RESOLVED] alternating a class

    Hi All,

    In one of my views I have the following:

    Code:
    <tbody> 
                @foreach (GRID.ViewModels.FrozenFoodsRiskViewModel risk in Model)
                { 
                 <tr class="t-master-row">
                    <td>@risk.Description</td>
                    <td>@risk.Rate</td>
                    <td>@risk.AgeDiscount</td>
                    <td>@risk.SumInsured</td>
                    <td class="t-last">@risk.TechnicalPrice</td>
                </tr>
                }
            </tbody>
    This works ok, but what I really want to do is on alternate rows, change the class of my <tr> element, any ideas how I do this at this level.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: alternating a class

    Use a 'for' loop instead of a 'foreach' loop. You can then use whether the loop counter is odd or even to decide which class to use on the row, e.g.
    Code:
    <tr class='@(i &#37; 2 == 0) ? "t-master-row" : "t-alt-master-row"'>
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width