82

I store encoded HTML in the database.

The only way i could display it correctly is :

<div class='content'>    
   @MvcHtmlString.Create(HttpUtility.HtmlDecode(Model.Content));
</div>

It's ugly. Is there any better way to do this?

jani
  • 1,344
  • 1
  • 9
  • 16

6 Answers6

157

Try this:

<div class='content'>    
   @Html.Raw(HttpUtility.HtmlDecode(Model.Content))
</div>
Amitabh
  • 59,111
  • 42
  • 110
  • 159
  • Thanks! This is better, but still not what I looking for. – jani Feb 17 '11 at 15:52
  • 8
    I ended up making an extension method according this idea. @Html.RawDecode(Model.Content) – jani Feb 17 '11 at 15:56
  • 3
    Just avoid ; in the end – Giovanny Farto M. Dec 29 '15 at 05:48
  • This works but its so dirty... ended up making an Extention method with this wrapped, can someone explain why doesnt the .Raw just work? – Egli Becerra Jul 22 '17 at 13:45
  • 1
    Just be aware that this will introduce XSS vulnerabilities, so make sure you trust the input. If you need to store and render formatted text from users, use more suitable markup languages like markdown or some custom XML like editors. – Matěj Groman Sep 06 '21 at 16:31
49

Use Html.Raw(). Phil Haack posted a nice syntax guide at http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx.

<div class='content'>
    @Html.Raw( Model.Content )
</div>
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 1
    Thank for the reply. But i think the Html.Raw() is 'display as it is, do not encode'. So if I use this, then I can't decode my html before I save it to the database. So it will display the user entered content without any 'security' check. So I think this is not the best solution. – jani Feb 17 '11 at 15:05
9

this is pretty simple:

HttpUtility.HtmlDecode(Model.Content)

Another Solution, you could also return a HTMLString, Razor will output the correct formatting:

in the view itself:

@Html.GetSomeHtml()

in controller:

public static HtmlString GetSomeHtml()
{
    var Data = "abc<br/>123";
    return new HtmlString(Data);
}
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
8

You can also simply use the HtmlString class

    @(new HtmlString(Model.Content))
Bellash
  • 7,560
  • 6
  • 53
  • 86
1

I store encoded HTML in the database.

Imho you should not store your data html-encoded in the database. Just store in plain text (not encoded) and just display your data like this and your html will be automatically encoded:

<div class='content'>
    @Model.Content
</div>
Mcanic
  • 1,304
  • 16
  • 22
0

I just got another case to display backslash \ with Razor and Java Script.

My @Model.AreaName looks like Name1\Name2\Name3 so when I display it all backslashes are gone and I see Name1Name2Name3

I found solution to fix it:

var areafullName =  JSON.parse("@Html.Raw(HttpUtility.JavaScriptStringEncode(JsonConvert.SerializeObject(Model.AreaName)))");

Don't forget to add @using Newtonsoft.Json on top of chtml page.

NoWar
  • 36,338
  • 80
  • 323
  • 498