Kai
Kai

Reputation: 2073

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.

Upvotes: 11

Views: 19173

Answers (4)

Craig Curtis
Craig Curtis

Reputation: 863

As of MVC 3 you can use:

@Html.Raw(model.Content)

Upvotes: 29

peterthegreat
peterthegreat

Reputation: 406

<%= Model.Content %>

The colon : is short for Html.Encode() while equal = simply post what is in the string.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

<%= model.Content %>

Be careful with this because it could open your site to XSS attacks.

Upvotes: 8

Gideon
Gideon

Reputation: 18491

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.

Upvotes: 5

Related Questions