2

From my controller I'm assigning either an up arrow (&uarr) or down arrow (&darr) with unicode to a ViewBag, that I want to use in the view.

Of course I want the output to become an arrow, but that doesn't work and the output becomes a string (e.g. &uarr).

controller:

...
ViewBag.Arrow = "↑";
...

view:

...
ViewBag.Arrow <br /> // <--- &uarr;
...
holyredbeard
  • 19,619
  • 32
  • 105
  • 171

3 Answers3

2

You must output using @Html.Raw(ViewBag.Arrow), this way your string will rendered verbatim, and not encoded (which is what the issue is).

Outputting using simply @ViewBag.Arrow results in asp.net mvc encoding your value for safe html output.

Matthew
  • 24,703
  • 9
  • 76
  • 110
2

It's better to use @MvcHtmlString.Create(ViewBag.Arrow) than @Html.Raw(.... See my answer here for the reason why: Writing/outputting HTML strings unescaped

Community
  • 1
  • 1
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
0

You could try something like this:

WebUtility.HtmlDecode(ViewBag.Arrow);
Simon Germain
  • 6,834
  • 1
  • 27
  • 42