I need to create a table with alternating row colors. Below is the code, but it does not work. Maybe a 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"; } }
Take an ad rowColoroutside for the operator.
rowColor
@{ var rowColor = "D9E6C4"; } @for (int i = 1; i <= 10; i++) { <tr style="background-color:@rowColor;" > <td> apoorva </td> </tr> if (@rowColor.Equals("#ffffff")) { rowColor = "#D9E6C4"; } else { rowColor = "#ffffff"; } }
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> }
CSS3, http://davidwalsh.name/css-tables-css3-alternate-row-colors
tr:nth-child(odd) { background-color:#ffffff; } tr:nth-child(even) { background-color:#D9E6C4; }
:
if (rowColor.Equals("#ffffff")) { rowColor = "#D9E6C4"; } else { rowColor = "#ffffff"; }
<tr style='background-color:@(i%2 == 0 ? "#D9E6C4":"#ffffff" );'> <td>apoorva</td> </tr>
: . :
var rowColor = "D9E6C4"; @for (int i = 1; i <= 10; i++) { <tr style="background-color:@rowColor;" > <td>apoorva</td> </tr> if (@rowColor.Equals("#ffffff")) { rowColor = "#D9E6C4"; } else { rowColor = "#ffffff"; } }
: @jcreamer898 i% 2 , .
css
tr: nth-child (even) {background: #CCC}tr: nth-child () {background: #FFF}
<html> <head> <title>Example Title</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('tr:even').addClass('alt-row-class'); }); </script> </head> <body>...</body> </html>
css:
.alt-row-class { background-color: green; }
- https://stackoverflow.com/posts/663122/edit
@{ string rowColor = "#D9E6C4"; <table> @for (int i = 1; i <= 10; i++) { <tr style="background-color:@rowColor;" > <td>apoorva</td> </tr> rowColor = rowColor == "#D9E6C4" ? "#FFFFFF" : "#D9E6C4"; } </table> }