11

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.

4 Answers 4

29

As of MVC 3 you can use:

@Html.Raw(model.Content)
Sign up to request clarification or add additional context in comments.

Comments

8
<%= model.Content %>

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

Comments

5

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.

1 Comment

From stackoverflow.com/questions/3382860/htmlstring-vs-mvchtmlstring : In ASP.Net 3.5 MVC 2.0, you should use MvcHtmlString. In .Net 4.0, you should use HtmlString. (which is simpler)
2
<%= Model.Content %>

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.