简体   繁体   English

如何从Razor变量指定数组到Dygraph

[英]How to specify array to Dygraph from Razor variable

I have a string[] of appropriate formatted strings in Session["PnlDataForChart"] 我在Session [“ PnlDataForChart”]中有适当格式的字符串[]

But I cannot display them in Dygraph. 但是我无法在Dygraph中显示它们。 I tried to inspect dataArray, but cannot see it in VS debugger, and am not sure how to do it in Firebug. 我试图检查dataArray,但是在VS调试器中看不到它,并且不确定如何在Firebug中进行操作。

Is this correctly set for Dygraph? Dygraph是否已正确设置?

From page source: 从页面来源:

        var dataArray = [];
                <div>
        dataArray.push("Date,Pnl");
        </div>
                <div>
        dataArray.push("2016/11/01 01:01,0.0000");
        </div>
                <div>
        dataArray.push("2016/11/01 01:02,0.0000");
        </div>
                <div>
        dataArray.push("2016/11/01 01:03,0.0000");
        </div>
                <div>
        dataArray.push("2016/11/01 01:04,0.0000");
        </div>
                <div>
        dataArray.push("2016/11/01 01:05,0.0000");
        </div>
                <div>
        dataArray.push("2016/11/01 01:06,0.0000");
        </div>

From View page: 从“查看”页面:

<div id="ExportDiv">

    <script type="text/javascript"
            src="dygraph-combined-dev.js"></script>

    <div id="graphdiv"></div>

    </script>
    <script type="text/javascript">
        var dataArray = [];
        @{
            foreach (var line in (string[])Session["PnlDataForChart"])
            {
                <div>
                dataArray.push("@line");
                </div>
            }
        }

        g = new Dygraph(
          document.getElementById("graphdiv"),
          dataArray
        );
    </script>
</div>

There is a lot of mess in your code. 您的代码中有很多混乱。 Just some of mistakes: 只是一些错误:

  1. After <div id="graphdiv"></div> you have </script> which is unnecessary. <div id="graphdiv"></div>您将拥有</script> ,这是不必要的。
  2. In foreach loop because you're in <script></script> block you cannot put <div></div> . 在foreach循环中,因为您在<script></script>块中,所以不能放置<div></div> In script block you can use only JavaScript syntax! 在脚本块中,您只能使用JavaScript语法!
  3. To access js variable from foreach loop you must use @: syntax, so you should do this in this way: @:dataArray.push("@line"); 要从foreach循环访问js变量,您必须使用@:语法,因此您应采用以下方式: @:dataArray.push("@line");

This is how whole solution should looks like: 这是整个解决方案的外观:

Controller: 控制器:

var dataArray = new string[] {
    "Date,Temperature",
    "2008-05-07,75",
    "2008-05-08,70",
    "2008-05-09,80"
};

Session["PnlDataForChart"] = dataArray;

And view: 并查看:

<script src="~/Scripts/dygraph.js"></script>

<div id="ExportDiv">
    <div id="graphdiv"></div>
</div>

<script type="text/javascript">
    var dataArray = [];

    @{
        foreach (var line in (string[])Session["PnlDataForChart"])
        {
            @:dataArray.push("@line");
        }
    }

    g = new Dygraph(document.getElementById("graphdiv"), dataArray.join('\n'));
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM