简体   繁体   English

如何循环使用 razor 和 javascript 一起 ASP.net 的数组

[英]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..尝试以下方法..

  1. Create Model with same structure and serialize it to JSON创建具有相同结构的 Model 并将其序列化为 JSON

  2. 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.

相关问题 在ASP.NET MVC中,如何将ASP.NET剃须刀字符串传递到javascript onclick函数中? - In ASP.NET MVC how do I pass in ASP.NET razor strings into a javascript onclick function? asp.net mvc - 如何在javascript中循环遍历模型数据 - asp.net mvc - how can you loop through model data in javascript ASP.Net core razor - 遍历 HTML 表,如何用逗号分隔值并发送到 JavaScript 函数 - ASP.Net core razor - looping through HTML table, how to comma separate the values and sent to JavaScript function ASP.NET MVC 3 Razor:初始化一个JavaScript数组 - ASP.NET MVC 3 Razor : Initialize a JavaScript array ASP.NET MVC 3 Razor:将JavaScript数组发送到控制器 - ASP.NET MVC 3 Razor : Send JavaScript array to controller 如何在JavaScript中遍历此数组? - How can I loop through this array in javascript? 我可以在我的asp.net代码中访问Javascript数组吗? - Can i access a Javascript array in my asp.net code? JavaScript-ASP.net-遍历ASP面板上的所有控件 - JavaScript - ASP.net - Loop through all controls on an asp pannel 如何在ASP.NET MVC 3中的Razor视图中编码嵌入式javascript? - How to encode embedded javascript in Razor view in ASP.NET MVC 3? 如何在javascript代码和asp.net razor mvc代码中利用相同的变量? - How do I utilize the same variable in both javascript code and asp.net razor mvc code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM