Jedidja
Jedidja

Reputation: 16970

How to display HTML stored in a database from an ASP.NET MVC view?

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.

Upvotes: 31

Views: 54219

Answers (3)

Nerdroid
Nerdroid

Reputation: 13996

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.

Upvotes: 2

Pure.Krome
Pure.Krome

Reputation: 87047

Try this:

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

More info here.

Upvotes: 55

whoblitz
whoblitz

Reputation: 1065

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))

Upvotes: 52

Related Questions