I\'m fairly used to razor now, but I can\'t understand why the following syntax is correct?
@Html.ActionLin
Just came across this bizarre behavior as well. Logically the following should work.
@(Model.IsTablet ? "data-options='is_hover: false'" : "")
but is rendered as
data-options="'is_hover:" false'=""
As Dan states this works correctly
@(Model.IsTablet ? "data-options=is_hover:false" : "")
rendering as the first example should.
data-options="is_hover:false"
but if you add a space in the attribute it breaks whatever weird stuff asp.net 4.0 is doing and it thinks your attribute ends at the space.
And this does not constitute html escaping as what is valid html syntax doesn't work and the whole point of razor is that the razor syntax should work with valid html not break it.
Just come across this so thought would answer - SLaks looks right with Html.Raw, but the OP is also correct in that the second method doesn't look to work - the "s get encoded.
My solution was:
<li@(active ? Html.Raw(" class=\"active\"") : null)>
Razor automatically HTML-escapes all code output.
You can prevent this by writing @Html.Raw(...)
Alternatively, you can put the quotes in the literal text:
<li class="@(active ? "active" : "")>
Your example works because you don't actually have any quotes.
The generated HTML source reads <li class=active>
.