I’m trying to output an HTML table using razor. The table itself works, but now I’m trying to add a class called “even” to every other row in the table:
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < ViewBag.MyList.Count; i++)
{
var myObject = ViewBag.MyList[i];
<tr @if (i % 2 == 0 ) { class="even" }>
<td>myObject.Column1</td>
<td>myObject.Column2</td>
<td>myObject.Column3</td>
</tr>
}
</tbody>
</table>
There’s obviously something wrong with that if-case inside the loop, but what is the correct way of writing this?
Please have a look at the razor syntax quick reference by phil haack
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
and scott guthries post about your problem
http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx