tags:

views:

139

answers:

2

I have a dynamic table like:

<tbody>
  <% foreach (var item in Model)
   {
  %>
      <tr>
       ......
      </tr>
  <% } %>
</tbody>

Then I want to change the row background color to different for neighbor row:

<tbody>
     <% int i = 0;
        foreach (var item in Model)
        {
     %>
     <%if (i++ % 2 == 0)
     { %>
       <tr style="background-color:Aqua">
     <%} else { %>
       <tr style="background-color:Fuchsia">
     <%} %>
     ....
      </tr>
  <% } %>
</tbody>

But it doesn't work. Or anotther way like: <tr style="background-color:<%...%>">, also not work. How to resolve this problem?

A: 

You are not incrementing your i variable in the loop.

kerchingo
Thank you. I have modified it, but my problem is runtime error.
KentZhou
Here is my error message: A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else
KentZhou
Thank you. Find out problem. i is used twice in the view.
KentZhou
A: 

Incremental i variable same this code.

Model.IndexOf(item)
takepara