How to get JSON data as a Razor Script Block

I'm just trying to get a JSON string from my controller (MVC3 using Razor syntax) in a client 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 that the data looks great, but when it ends in the browser, it has escaping code around all the data that is not good.

 &var initialData = {&quot;DateId&quot;:32,&quot;Scores&quo .... 

It should be easy! What am I doing wrong?

+4
source share
1 answer

Use @Html.Raw() to prevent data encoding as follows:

 <script type="text/javascript"> var initialData = @Html.Raw(ViewBag.JasonData); </script> 
+8
source

All Articles