我从JSON ASP.NET MVC发送一个字符串,在我看来,我收到了JSON字符串,并使用ViewBag.string分配了一个脚本。 我的问题是在视图中,字符串类型值正在用me&替换引号。 QUOT;这导致我的错误。
控制器:
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:`[{"Planta":"CO","Mes":3,"Cantidad":2},{"Planta":"EP","Mes":1,"Cantidad":1},{"Planta":"R1","Mes":1,"Cantidad":2},{"Planta":"RM","Mes":3,"Cantidad":3},{"Planta":"RQ","Mes":3,"Cantidad":1},{"Planta":"TY","Mes":1,"Cantidad":1},{"Planta":"TY","Mes":3,"Cantidad":3},{"Planta":"TY","Mes":4,"Cantidad":2},{"Planta":"ZB","Mes":3,"Cantidad":1},{"Planta":"ZB","Mes":4,"Cantidad":1}],`
xkey: 'Planta',
ykeys: 'Cantidad',
labels: 'Mes'
})
</script>
答案 0 :(得分:2)
Razor假设内容是HTML并且正在逃避您的JSON。使用Html.Raw
可以停止此行为。
...
data:@Html.Raw(ViewBag.datos)
...