Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC how to display html tags as html?

I display some text in the view:

....
 <%: model.Content %>
....

my model.Content contains html tags, and I want to display them not as text, but as html. How to do it?

Thanks.

like image 820
Kai Avatar asked Nov 29 '10 07:11

Kai


People also ask

Which ASP.NET MVC object generates the HTML output that displays in a web browser?

The HTML content (data) will be displayed using Html. Raw Helper method in ASP.Net MVC Razor. In this article I will explain with an example, how to display HTML content (data) from Model in View in ASP.Net MVC Razor.

How add HTML to MVC?

Step 1: Right click on the "Controllers" folder and add "LoadHtml" controller. Copy and paste the following code. Step 2: Right click on the "Index" action method in the "LoadHtmlController" and add "Index" view.

What is HTML raw in MVC?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. Configuring Bundles. Please refer the following article for complete information on how to configure Bundles in ASP.Net MVC project. Using Bundles (ScriptBundle) in ASP.Net MVC Razor.

What is HTML DisplayFor?

DisplayFor<TModel,TValue>(HtmlHelper<TModel>, Expression<Func<TModel,TValue>>, String, String, Object) Returns HTML markup for each property in the object that is represented by the specified expression, using the template, an HTML field ID, and additional view data.


2 Answers

As of MVC 3 you can use:

@Html.Raw(model.Content)
like image 78
Craig Curtis Avatar answered Sep 30 '22 16:09

Craig Curtis


Use:

<%: MvcHtmlString.Create(model.Content) %>

or

<%= model.Content %>

Because <%: does Html encoding, while <%= doesn't.

MvcHtmlString.Create creates a 'save' Html string, which<%: takes and prints out as is.

like image 42
Gideon Avatar answered Sep 30 '22 16:09

Gideon