How to use Html.Raw() in MVC?

Forums ASP.NET MVCHow to use Html.Raw() in MVC?
Staff asked 2 years ago

Answers (1)

Add Answer
Priyank Parekh Marked As Accepted
Staff answered 2 years ago

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

@Html.Raw(ViewBag.div);

Renders

<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.

Subscribe

Select Categories