How to style alternating rows in asp.net mvc

Posted by YeahStu on Stack Overflow See other posts from Stack Overflow or by YeahStu
Published on 2009-10-19T02:38:33Z Indexed on 2010/04/10 17:13 UTC
Read the original article Hit count: 508

Filed under:
|
|
|

I am using a <% foreach ...%> loop to list a collection of items on my website.

I want to add a different background color for the style of alternating rows in my list. I have found a way to do this but I am not satisfied with it as it seems like a hack.

Here is what I have done to solve it so far:

<table>
  <% int counter = 0; 
      foreach (var item in Model)
      {
          counter++;

          if (counter % 2 == 0)
          {
   %>
          <tr class="colorfull">
          <% }
          else
          { %>
          <tr>
          <% } %>
...

Is there a best practice that I am missing that people are using for this scenario in ASP.NET MVC?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about jQuery