I have just gone through some conversion pains in merging my code from ASP MVC1 to ASP.NET MVC3 using the Razor view engine. Here is the biggest one. All of my helpers started to be HTML encoded and for hours I could not figure out how to render HTML from the server at all. I was also rendering some HTML from the database, which was also getting encoded. What was wired was that this was especially happening when using the Razor view engine, which I otherwise love...
So this is the solution, in MVC3 there is a new type for HTML helpers called MvcHtmlString which should be used rather than just string, which gets encoded by default. So your helpers should always look like this:
public static MvcHtmlString doSomething(this HtmlHelper htmlHelper, string html)
{
//do something with the HTML
return MvcHtmlString.Create(html);
}
In addition, you have some HTML coming from another source like a database, you have to wrap your Razor tag in the MvcHtmlString.Create function like this:
@MvcHtmlString.Create(View.pageHtml)
Hope this helped :)
Shahab
12/8/2010 9:28 PM
Hi!
Wow! You saved my life! Thanks mate...thanks a lot!
I was hitting my head to the wall!
Thanks!