Razor encodes quoted html attributes

I would like to conditionally display the appropriate HTML for the client using this construct:

<input type="button" value="Foo" @(string.IsNullOrEmpty(Model.Identifier) ? string.Format("title={0} disabled=disabled", "Lorem ipsum") : "onclick=window.open('http://www.google.com'); return false;") /> 

This is the result I get:

 <input type="button" value="Foo" title=&quot;Lorem ipsum&quot; disabled=disabled /> 

I tried many constructs of Html.Raw (), but nothing helps. How to correctly display unencrypted HTML with quotes instead of html entities?

+4
source share
2 answers

Try it. Just tried it and it worked for me. The difference lies in the single number of quotes and Html.Raw around everything.

 <input type="button" value="Foo" @Html.Raw(string.IsNullOrEmpty(Model.Identifier) ? string.Format("title='{0}' disabled='disabled'", "Lorem ipsum") : "onclick='window.open(\"http://www.google.com\"); return false;'") /> 
+4
source

I think adding a single quote is enough

 ..."title='{0}' disabled='disabled'"... 
-2
source

Source: https://habr.com/ru/post/1482030/


All Articles