Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC4 - display HTML containing string as raw HTML

I have a string, read from a database, that contains HTML that I want to output. Despite applying HttpUtility.HtmlDecode(), the View always renders the string as encoded HTML (i.e. &lt;SPAN&gt; instead of <SPAN>).

I am using:

string test = WebUtility.HtmlDecode(myStr);
<span>@test</span>

I have tried:

string test = HttpUtility.HtmlDecode(myStr);
<span>@test</span>

<span>@HttpUtility.HtmlDecode(myStr)</span>
like image 626
John 'Mark' Smith Avatar asked May 31 '26 09:05

John 'Mark' Smith


1 Answers

Use Html.Raw()

@Html.Raw("<span>Hello</span>")

All the output from helpers and other elements in Razor are put through HttpUtility.HtmlEncode, unless they implement IHtmlString. But your best option here is using Html.Raw()

like image 194
trailmax Avatar answered Jun 03 '26 00:06

trailmax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!