I need to design a table with alternating row colors. Below is written code but its not working. May be some syntax issue for MVC. Please suggest.
@for (int i = 1; i <= 10; i++)
{
var rowColor = "D9E6C4";
<tr style="background-color:@rowColor;" >
<td>apoorva</td>
</tr>
if (@rowColor.Equals("#ffffff"))
{
rowColor = "#D9E6C4";
}
else
{
rowColor = "#ffffff";
}
}
CSS3 example taken from http://davidwalsh.name/css-tables-css3-alternate-row-colors
tr:nth-child(odd) { background-color:#ffffff; }
tr:nth-child(even) { background-color:#D9E6C4; }
Try...
@for (int i = 1; i <= 10; i++)
{
string rowColor;
if(i % 2 == 0)
{
rowColor = "D9E6C4";
}
else
{
rowColor = "ffffff";
}
<tr style="background-color:#@rowColor;" >
<td>apoorva</td>
</tr>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With