MVC:迭代javascript中Viewbag阵列 [英] MVC: Iterating a Viewbag array in javascript
本文介绍了MVC:迭代javascript中Viewbag阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们的目标是从 ViewBag.Array
中的数据得到一个JavaScript数组。该数据在控制器计算的,所以我不能从数据库中取直它。我需要的数据绘制的图表与jqplot。 code:
The goal is to get the data from the ViewBag.Array
to a Javascript array. The data is calculated in the controller so I cannot fetch it straight from the database. I need the data to draw a chart with jqplot. Code:
for(i = 0; i < @ViewBag.Array.Length; i++)
{
jScriptArray[i] = @ViewBag.Array[i];
}
问题是'我'在目前情况下不存在在 @ ViewBag.Array [I]
但在<$ C $没有问题C> jScriptArray [I] 。任何帮助是AP preciated。
The problem is "'i' does not exist in the current context" in the @ViewBag.Array[i]
but has no problems in the jScriptArray[i]
. Any help is appreciated.
推荐答案
您可以尝试以下操作:
var array = @Html.Raw(Json.Encode(@ViewBag.Array));
for(var i = 0; i < array.length; i++) {
jScriptArray[i] = array[i];
}
这篇关于MVC:迭代javascript中Viewbag阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文