I'm just trying to get a JSON string from my controller (MVC3 using Razor syntax) into the clients browser...
In My Controller I do this with a simple object (test) that contains an int and a list.
var jasonData = new JavaScriptSerializer().Serialize(test);
ViewBag.JasonData = jasonData;
In the view I do this:
<script type="text/javascript">
var initialData = @(ViewBag.JasonData);
</script>
Visual Studio shows the data looking fine but when it ends up in the Browser it has the escaping code around all the data which is not good.
&var initialData = {"DateId":32,"Scores&quo ....
This should be easy! What am I doing wrong??
Use @Html.Raw()
to prevent the data from being encoded, as follows:
<script type="text/javascript">
var initialData = @Html.Raw(ViewBag.JasonData);
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With