OdeToCode IC Logo

Razor Tip #1

Thursday, January 6, 2011

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>