剃刀MVC填充的Javascript阵列模式阵列 [英] Razor MVC Populating Javascript array with Model Array

查看:110
本文介绍了剃刀MVC填充的Javascript阵列模式阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载JavaScript数组从我的模型数组。它在我看来,这应该是可能的。

I'm trying to load a JavaScript array with an array from my model. Its seems to me that this should be possible.

既不下面的方式进行工作。

Neither of the below ways work.

无法创建JavaScript变量一个JavaScript循环和增量通过示范阵列

Cannot create a JavaScript loop and increment through Model Array with JavaScript variable

        for(var j=0; j<255; j++)
        {
            jsArray = (@(Model.data[j])));
        }

无法创建一个剃刀循环,JavaScript是超出范围

Cannot create a Razor loop, JavaScript is out of scope

        @foreach(var d in Model.data)
        {
            jsArray = d;
        }

我可以得到它一起工作。

I can get it to work with

        var jsdata = @Html.Raw(Json.Encode(Model.data)); 

但我不知道为什么我应该使用JSON。

But I don't know why I should have to use JSON.

此外,而在那一刻,我这个限制为255字节。在未来,可能会遇到很多的MB。

Also while at the moment I'm restricting this to 255 bytes. In the future it could run into many MBs.

推荐答案

这是可能的,你只要通过收集剃须刀需要循环

This is possible, you just need to loop through the razor collection

<script type="text/javascript">

    var myArray = [];

    @foreach (var d in Model.data)
    {
        @:myArray.push("@d");
    }

    alert(myArray);

</script>

希望这有助于

这篇关于剃刀MVC填充的Javascript阵列模式阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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