2014-04-08 20 views
1

我從JSON ASP.NET MVC發送字符串,在我看來我收到JSON字符串,並使用ViewBag.string分配腳本。 我的問題是在視圖中,字符串類型值是用我的&來代替引號「這是我造成的錯誤。JSON字符串越獄

控制器:

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> 

這是產生我跑的觀點

代碼
<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> 
+0

您是否嘗試過使用JSON.NET? – abatishchev

回答

1

剃刀是假設的內容是HTML和被轉義您JSON。使用Html.Raw停止此行爲。

... 
data:@Html.Raw(ViewBag.datos) 
...