HTML with in-line Razor

This little gotcha keeps biting me in the ass every time I move away from Razor for a while. So I’ve decided to blog so this time I’ll remember.

When you want to insert some razor syntax in-line within a HTML tag. Make sure you include the @ delimiter on any further statement.


Following example attempts to add a class based on a bool value in the model.

The class show does not render:

<div class="@if(Model.Show) { Html.Raw("show") }"> 

The Html.Raw helper method is returning a MvcHtmlString however it will not output the value unless you tell it to by prefixing the statement with @. Now it renders !

<div class="@if(Model.Show) { @Html.Raw("shoe"); } }">

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s