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"); } }">