Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 924
Jedidja Avatar asked Nov 18 '08 22:11

Jedidja


People also ask

How add HTML to MVC?

There are two ways in MVC to create custom Html helpers as below. We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

Does MVC use HTML?

It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application. We can build an ASP.NET MVC application without using them, but HTML Helpers helps in the rapid development of a view.


2 Answers

Try this:

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

More info here.

like image 113
Pure.Krome Avatar answered Oct 12 '22 00:10

Pure.Krome


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)) 
like image 35
whoblitz Avatar answered Oct 12 '22 00:10

whoblitz