MVC:在 javascript 中迭代 Viewbag 数组 [英] MVC: Iterating a Viewbag array in javascript

查看:43
本文介绍了MVC:在 javascript 中迭代 Viewbag 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是将数据从 ViewBag.Array 获取到 Javascript 数组.数据是在控制器中计算的,所以我无法直接从数据库中获取它.我需要数据来用 jqplot 绘制图表.代码:

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] 中的'i' 在当前上下文中不存在",但 jScriptArray[i] 中没有问题>.任何帮助表示赞赏.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆