How does Html.Raw MVC helper work?

Because encoded characters are HTML, and the Raw version of that string is the encoded one.

Html.Raw renders what it is given without doing any html encoding, so with ViewBag.div = "<div> Hello </div>";:



<div> Hello </div>

However, when you have encoded characters in there, such as ViewBag.Something = "&gt;"; the raw version of that is &gt;. To get back to actual html you need to Html.Raw(HttpUtility.HtmlDecode(EncodedContent)); as you’ve said.

If Html.Raw did do the decoding then it would be confusing, and we would need something that didn’t do it. 😉

Leave a Comment