31

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.

Hrvoje
  • 13,566
  • 7
  • 90
  • 104
Jedidja
  • 16,610
  • 17
  • 73
  • 112

3 Answers3

55

Try this:

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

More info here.

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • 31
    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
52

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,538
  • 16
  • 59
  • 73
whoblitz
  • 1,065
  • 1
  • 11
  • 17
2

You want to use @Html.Raw(str).

See MSDN for more information.

Returns markup that is not HTML encoded.

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

Nerdroid
  • 13,398
  • 5
  • 58
  • 69