3

I am sending a string from JSON ASP.NET MVC, in my view I receive the JSON string and I assign a Script using ViewBag.string. My problem is in the view, the string type values ​​are replacing the quotes with me & quot; which is causing me errors.

Controller:

var jss = new JavaScriptSerializer();
string retorno = jss.Serialize(chart.ToList());
ViewBag.datos = retorno;

ViewL

<script type="text/javascript">
    new Morris.Bar({
        element: 'BarChart',
        data:@ViewBag.datos,
        xkey: 'Planta',
        ykeys: 'Cantidad',
        labels: 'Mes'
    })
</script>

This is the code that generates me to run the view

<script type="text/javascript">
    new Morris.Bar({
        element: 'BarChart',
        data:`[{&quot;Planta&quot;:&quot;CO&quot;,&quot;Mes&quot;:3,&quot;Cantidad&quot;:2},{&quot;Planta&quot;:&quot;EP&quot;,&quot;Mes&quot;:1,&quot;Cantidad&quot;:1},{&quot;Planta&quot;:&quot;R1&quot;,&quot;Mes&quot;:1,&quot;Cantidad&quot;:2},{&quot;Planta&quot;:&quot;RM&quot;,&quot;Mes&quot;:3,&quot;Cantidad&quot;:3},{&quot;Planta&quot;:&quot;RQ&quot;,&quot;Mes&quot;:3,&quot;Cantidad&quot;:1},{&quot;Planta&quot;:&quot;TY&quot;,&quot;Mes&quot;:1,&quot;Cantidad&quot;:1},{&quot;Planta&quot;:&quot;TY&quot;,&quot;Mes&quot;:3,&quot;Cantidad&quot;:3},{&quot;Planta&quot;:&quot;TY&quot;,&quot;Mes&quot;:4,&quot;Cantidad&quot;:2},{&quot;Planta&quot;:&quot;ZB&quot;,&quot;Mes&quot;:3,&quot;Cantidad&quot;:1},{&quot;Planta&quot;:&quot;ZB&quot;,&quot;Mes&quot;:4,&quot;Cantidad&quot;:1}],`
        xkey: 'Planta',
        ykeys: 'Cantidad',
        labels: 'Mes'
    })
</script>
1
  • Have you tried JSON.NET instead? Commented Apr 8, 2014 at 22:31

1 Answer 1

4

Razor is assuming the content is HTML and is escaping your JSON. Use Html.Raw to stop this behaviour.

...
data:@Html.Raw(ViewBag.datos)
...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.