2

I have a site built with ASP.NET MVC. I have a string in my view model that looks like this:

ViewBag.Text = "{\"1\":{\"1\":\"John\",\"2\":\"Bill\",\"3\":\"Paul\"},\"3\":{}}";

I want to output this into my view like this:

<input id='myHiddenInput' type='hidden' value='@ViewBag.Text' />

When the view gets rendered, the hidden element looks like this:

<input id='myHiddenInput' type='hidden' value='{&quot;1&quot;:{&quot;1&quot;:&quot;John&quot;,&quot;2&quot;:&quot;Bill&quot;,&quot;3&quot;:&quot;Paul&quot;},&quot;3&quot;:{}}' />

How do I update the view so that the output looks like the following:

<input id='myHiddenInput' type='hidden' value='{"1":{"1":"John";,"2":"Bill","3":"Paul"},"3":{}}' />

I know this seems goofy. This really is what i need to do though.

xam developer
  • 1,923
  • 5
  • 27
  • 39

1 Answers1

12

You can use Html.Raw to output the unencoded data:

<input id='myHiddenInput' type='hidden' value='@Html.Raw(ViewBag.Text)' />

Here is a link to dotnetfiddle. You can see the output in text field, but there is also a hidden field with the same unencoded information.

dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • Oddly, that is not working for me. I do not know why. Any ideas? – xam developer Jun 15 '15 at 12:13
  • @xamdeveloper I updated answer with working edition on dotnetfiddle. It is hard to tell why it is not working in your case without seeing the full code. Maybe looking at fiddle will help you resolve the issues – dotnetom Jun 15 '15 at 12:19
  • 1
    @dotnetom, thank you so much for introducing me to dotnetfiddle! Had no idea it existed for mvc! :) – AmmarCSE Jun 15 '15 at 12:26