2011-10-18 28 views
1

私は剃刀を使用してHTMLテーブルを出力しようとしています。テーブル自体は動作しますが、今私は、テーブル内の他のすべての行に「偶数」と呼ばれるクラスを追加しようとしています:ASP.NET MVC Razor - 内部の場合

<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> 

ありループ内その場合、ケースと間違って何かが明らかだが、何がありますこれを書いている正しい方法?

答えて

11
<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) { <text>class="even"</text> }}> 
       <td>myObject.Column1</td> 
       <td>myObject.Column2</td> 
       <td>myObject.Column3</td> 
      </tr> 
     } 
    </tbody> 
</table> 

フィルハークによってかみそりの構文のクイックリファレンス

http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

を見ているとスコットguthriesがあなたの問題

http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

+0

ありがとうdknaack、うまくいきます!そして、にリンクしてくれてありがとう。 :) – haagel

2

理由だけではなく、CSS3を使用しないことについて投稿してくださいゼブラストライプあなたのテーブルに?

tr:nth-child(2n+1) { 
    background-color: #99ff99; 
} 
関連する問題