Razor Tip #1

The Razor parser is smart. For example, if you want to display an email address, you can enter the following:

<p>
    Serial@Model.com
</p>

... and Razor will happily output Serial@Model.com into the output - it recognizes the email address.

But what if you want to output a serial number?

<p>
    Serial@Model.SerialNumber
</p>

Unfortunately, Razor still thinks you have an email address and outputs "Serial@Model.SerialNumber". In a few rare cases you have to be more explicit and let the parser know where the C# code begins and ends.

<p>
    <text>Serial</text>@Model.SerialNumber
</p>

But a prettier approach is to use an “explicit code nugget” with parentheses:

<p>
    Serial@(Model.SerialNumber)
</p>

Print | posted @ Thursday, January 06, 2011 9:12 AM

Comments on this entry:

Gravatar # re: Razor Tip #1
by Shahnawaz at 1/6/2011 12:13 PM

Nice tip!

Waiting for your next .net tip(s).

Thanks!
  
Gravatar # re: Razor Tip #1
by Eber Irigoyen at 1/6/2011 1:51 PM

won't that omit the @ in the last example?
  
Gravatar # re: Razor Tip #1
by Eber Irigoyen at 1/6/2011 1:52 PM

nevermind, that's what you want
  
Gravatar # re: Razor Tip #1
by scott at 1/6/2011 2:03 PM

@Eber - right, trying to display:
"Serial779-332X"
  
Gravatar # re: Razor Tip #1
by luke at 1/19/2011 8:27 AM

If you did want an @ you can escape the @ by entering @@

so would have:
Serial@@@(Model.SerialNumber)
  
Gravatar # re: Razor Tip #1
by scott at 1/19/2011 9:07 PM

@luke: Good one.
  
Comments have been closed on this topic.
Scott Allen
Posts - 869
Comments - 4493
Stories - 14