shytikov
shytikov

Reputation: 9538

@ statement in razor templates shows HTML symbols instead of quotes

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=&quot;prettyPrint()&quot;>

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

Answers (1)

Michal Klouda
Michal Klouda

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

Related Questions