[英]How can I loop through an array with razor and javascript together ASP.net
I am currently using ASP.NET MVC, I am using javascript to create a graph that represents data in my models.我目前正在使用 ASP.NET MVC,我正在使用 javascript 创建一个表示模型中数据的图表。 The Javascript is expecting the data in this format:
Javascript 需要这种格式的数据:
series: [{
name: 'Bench',
data: [
10, 20, 30
]
}],
However, I am trying to use razor to loop through my array in the data field as so:但是,我正在尝试使用 razor 在数据字段中循环遍历我的数组,如下所示:
series: [{
name: 'Bench',
data: [
@for (int j = 0; j < numArry.Length; j++)
{
numArry[j] + ",";
}
]
}],
My issue is that the comma that separates my integers is not accepted by razor syntax.我的问题是 razor 语法不接受分隔我的整数的逗号。 I need to comma to be repeated after every array integer otherwise the graph will only contain the last integer in the array.
我需要在每个数组 integer 之后重复逗号,否则该图将仅包含数组中的最后一个 integer。
series: [{
name: 'Bench',
data: [
@numArry[0]
,
@numArry[1]
,
@numArry[2]
,
@numArry[3]
]
}],
This is my current solution that does work however, I can enter every element of the array manually.这是我当前有效的解决方案,但是我可以手动输入数组的每个元素。
Try it following ways..尝试以下方法..
Create Model with same structure and serialize it to JSON创建具有相同结构的 Model 并将其序列化为 JSON
Move the for loop outside of the series and declare a variable before the series code as below to get comma separated value.将 for 循环移到系列之外,并在系列代码之前声明一个变量,如下所示以获得逗号分隔值。
@ {
var str = String.Join(",", numArry);
}
series: [{ name: 'Bench', data: [ { @ {str } ] }],系列:[{名称:'Bench',数据:[{@{str}]}],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.