Note that there are some explanatory texts on larger screens.

plurals
  1. POEven and odd table rows with Razor
    text
    copied!<p>I'm using the Razor view engine with MVC 3 and I'm trying to make even and odd rows have different classes in a table.</p> <p>So far I've got this</p> <pre><code>@{ var odd = true; } @foreach(var userLot in Model) { if (!odd) { &lt;tr id="lot@userLot.Id" class="even"&gt; else &lt;tr id="lot@userLot.Id" class="odd"&gt; } &lt;td&gt;@userLot.Id&lt;/td&gt; &lt;td&gt;@userLot.Description&lt;/td&gt; &lt;td&gt;@userLot.Carat&lt;/td&gt; &lt;td class="averageBid"&gt;@userLot.AverageBid&lt;/td&gt; &lt;td class="rank"&gt;@userLot.Rank&lt;/td&gt; &lt;td class="currentBid"&gt;@userLot.CurrentBid&lt;/td&gt; &lt;td style="width: 200px; height: 30px;" class="tdWithBidInput"&gt;&lt;input type="text" style="display: none" /&gt;&lt;/td&gt; &lt;/tr&gt; @{ odd = !odd; } } </code></pre> <p>This is giving me endless trouble with the stupid view engine unable to figure out what is markup and what is code. I've tried wrapping the tr opening tags in a text directive, but then the stupid view engine moans about the closing tr tags. If I then wrap the closing tr tag in a text directive the stupid view engine moans that the text directive has no opening tag.</p> <p>Just to be clear, this</p> <pre><code>&lt;text&gt;&lt;/ tr&gt;&lt;/text&gt; </code></pre> <p>gives an error that the text tag has no matching opening tag. Lovely.</p> <p>How do I write this so that Razor doesn't give an error?</p> <p>Please don't recommend a JavaScript solution, I'm trying to get around the Razor issues here.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload