Reputation: 9538
I've noted that @
statement in Razor templates converts quotes in strings into HTML symbols.
How can I show correct HTML attribute then? Sample code:
<body@(ViewBag.Highlight == true ? " onload=\"prettyPrint()\"" : "")>
result:
<body onload="prettyPrint()">
That's completely incorrect. How can i achieve normal:
<body onload="prettyPrint()">
in my case?
I've tried HtmlString
object from this answer. But it's impossible to convert HtmlString
to string
even with explicit type cast.
Upvotes: 0
Views: 697
Reputation: 14521
You will need to use Html.Raw()
. Try it this way:
@Html.Raw(String.Format("<body{0}>", ViewBag.Highlight == true ? " onload=\"prettyPrint()\"" : ""))
As the documentation says:
Returns markup that is not HTML encoded.
Upvotes: 2