12

@ in asp.net mvc 3 preview 1 automaticly encodes html, is there an alternative way to let there be html?

think of this scenario:

@view.BestSitesEver.Replace("stackoverflow", "<h1>StackOverflow</h1>")

That would just print out: <h1>stackoverflow</h1>

1
  • What would this "stackoverflow" parameter be? A div? Commented Jan 25, 2011 at 20:01

2 Answers 2

19

You can use this

@MvcHtmlString.Create(site.Replace("stackoverflow", "<h1>stackoverflow</h1>"))

This will output the html string without encoding

@(new HtmlString(site.Replace("stackoverflow", "<h1>stackoverflow</h1>")))

And with Erik Porter's comment

Sign up to request clarification or add additional context in comments.

1 Comment

As of MVC 3 you don't need to use MvcHtmlString anymore. @(new HtmlString("<h1>StackOverflow</h1>")) will work just fine. Any implementation of IHtmlString will work though. We're considering a helper or shortcut that would create the new HtmlString for you in the future.
14

A little bit late now but there's a convenient extension method in MVC3: Html.Raw():

@Html.Raw(site.Replace("stackoverflow", "<h1>stackoverflow</h1>"))

1 Comment

Is there any way of getting this to work within a declarative helper (ie - in App_Code)? Looks like all Html helpers are broken in there.

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.