How to use Html.Raw() in MVC?
Answers (1)
Add AnswerHtml.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 = ">";
the raw version of that is >
. 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.