How to format inline razor variables
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 {   } saying it expects a semicolon ;. Here's the code:
@foreach(var row in db.Query(selectQueryString)){
<tr>
<td>@row.ACCT    </td>
<td>@row.QuoteStart    </td>
<td>@row.VIN     </td>
<td>@{ if (String.IsNullOrEmpty(row.AmountFinanced) == true)
{   } else
{String.Format("{0:0,0.00}",row.AmountFinanced)     }
} </td>
<td>@row.开发者_开发问答Step     </td>
</tr>
}
You need to wrap your
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
is supposed to be code.
@foreach(var row in db.Query(selectQueryString)){
<tr>
<td>@row.ACCT   </td>
<td>@row.QuoteStart </td>
<td>@row.VIN </td>
<td>@{ if (String.IsNullOrEmpty(row.AmountFinanced) == true)
{ <text> </text> } else
{ @String.Format("{0:0,0.00}",row.AmountFinanced) <text> </text> }
} </td>
<td>@row.Step </td>
</tr>
}
精彩评论