30

I have HTML code edited by FCKEditor stored in a database and would like to display (well render) it onto a view. So, for instance, something stored as:

<>pre<>This is some sample text<>pre</&gt

Will be displayed to the user as:

This is some sample text

(With the appropriate style for pre-formatted-text)

The view already has the required string to display from ViewData, I'm just not sure what the best way to show it to the user is.

Harvey
  • 6,913
  • 3
  • 52
  • 54
Jedidja
  • 15,384
  • 16
  • 71
  • 109

3 Answers3

55

try

<%= System.Web.HttpUtility.HtmlDecode(yourEncodedHtmlFromYouDatabase) %>

more info here @ MSDN online.

hth!

Pure.Krome
  • 78,923
  • 102
  • 356
  • 586
  • 28
    this doesn't work for Razor, for Razor use @Html.Raw(System.Web.HttpUtility.HtmlDecode(Model.yourhtmlvalue)) – Niraj Nov 28 '12 at 07:27
  • 4
    Checking the date of my answer .. it was PRE Razor :P – Pure.Krome Nov 28 '12 at 22:55
  • 2
    Thanks for bringing the date to my attention Pure, I have quoted just for somebody's info, its true Razor was not in existence when u posted your answer. – Niraj Dec 03 '12 at 06:36
50

The answer provided by Pure.Krome is flawless for MVC2, but consider Razor syntax:

@Html.Raw(System.Web.HttpUtility.HtmlDecode(Model.yourEncodedHtmlFromYourDatabase))

Alternatively,

@Html.Raw(Server.HtmlDecode(Model.yourEncodedHtmlFromYourDatabase))
Suhaib Janjua
  • 3,273
  • 13
  • 52
  • 68
whoblitz
  • 1,015
  • 1
  • 9
  • 17
2

you want to use @Html.Raw(str)

See MSDN for more

Returns markup that is not HTML encoded.

This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML.

Community
  • 1
  • 1
Nerdroid
  • 11,584
  • 4
  • 52
  • 62