Question

Razor does a great job of knowing what you want to do when it's simple. I just want to format a variable from a query and am a bit confused. Everything works great, except the one line with the if string isnull statement in it. The compiler fails on the line with the { &nbsp } saying it expects a semicolon ;. Here's the code:

@foreach(var row in db.Query(selectQueryString)){ 
<tr> 
       <td>@row.ACCT &nbsp &nbsp</td> 
       <td>@row.QuoteStart &nbsp &nbsp</td> 
       <td>@row.VIN &nbsp &nbsp </td>
       <td>@{ if (String.IsNullOrEmpty(row.AmountFinanced) == true) 
                 { &nbsp } else 
                 {String.Format("{0:0,0.00}",row.AmountFinanced) &nbsp &nbsp }
            } </td>
       <td>@row.Step &nbsp &nbsp </td>


</tr> 
} 
Was it helpful?

Solution

You need to wrap your &nbsp;s in a <text></text> block. This forces the parser to escape back into html because when you're in a {} block the parser will assume that the &nbsp; is supposed to be code.

@foreach(var row in db.Query(selectQueryString)){ 
<tr> 
       <td>@row.ACCT &nbsp &nbsp;</td> 
       <td>@row.QuoteStart &nbsp; &nbsp;</td> 
       <td>@row.VIN &nbsp; &nbsp; </td>
       <td>@{ if (String.IsNullOrEmpty(row.AmountFinanced) == true) 
                 { <text>&nbsp;</text> } else 
                 { @String.Format("{0:0,0.00}",row.AmountFinanced) <text>&nbsp; &nbsp;</text> }
            } </td>
       <td>@row.Step &nbsp; &nbsp; </td>
</tr> 
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top