Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 { &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> 
} 
like image 286
Knox Avatar asked Nov 22 '10 21:11

Knox


People also ask

How do you declare variables in Razor?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.

What is @: In Cshtml?

This operator is useful in conjunction with other Razor server side operators when you want to output something as a literal text. For example: @if (model. Foo) { @:Some text to be written directly. }

How do you give ID in Razor syntax?

Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.

Is Cshtml a Razor?

cshtml files are razorpages or MVC views, they does not contain any C#-written client-side code. If you wan to do so, you must use JavaScript. However, a . razor file, also know as a Razor component, can have C# written in it and run on client's browser.


1 Answers

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> 
} 
like image 148
Buildstarted Avatar answered Oct 09 '22 04:10

Buildstarted