使用MVC/Razor传递C#变量以与Dygraph一起使用 [英] Passing C# variable using MVC/Razor for use with Dygraph

查看:45
本文介绍了使用MVC/Razor传递C#变量以与Dygraph一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,我敢肯定这很简单.我是Web开发的新手,遇到了一个我无法解决的小问题.

我在MVC视图中有此代码..

  @ {var data =";var count = 1;foreach(ViewModel.Data中的var项){数据+ = count.ToString()+," + item.Reading.ToString()+"\ n";数++;} 

}

我想将此数据"变量传递给一个很小的脚本,以便使用Dygraph创建图形.这是我目前拥有的,并且相信应该可以,但是没有.

 < script type ="text/javascript">g = new Dygraph(document.getElementById("readingGraph"),@data,{标签:[样本编号",阅读"]});</script> 

我知道脚本本身可以工作.如果我将一些CSV值硬编码到@data所在的字符串中,则会弹出图形,一切都很好.使用@data时,该图形将不会加载,并且脚本将完全不会运行(我在其中放置了一个alert('hi')来查看它是否会弹出,但没有弹出).

此外,我知道该字符串已正确构建,因为我已经在Visual Studio中设置了断点并进行了检查.

任何帮助都是好人.预先感谢.

解决方案

我发现了问题.JS文字字符串不能跨越多行(这就是我通过C#创建的"data"变量向其提供信息的方式.)

我必须在C#代码中添加一个'@'才能将其转换为字符串文字..

  foreach(CompletePreview.BatteryData中的可变项){数据+ = count.ToString()+," + item.Voltage + @"\ n";//注意"\ n"前面的新@数++;} 

必须更改脚本,以将文字'\ n'替换为可以在JS中分解的文字.

  g = new Dygraph(document.getElementById("voltGraph"),jsgraphData.replace('\ n','\ n'),{标签:[样本号",电压"]});警报(图应跟随"); 

感谢大家的帮助.非常感谢.

So here is my problem, and I'm sure it's something very simple. I'm relatively new to Web Development and I'm running into a little problem that I just can't figure out.

I have this code in an MVC view. . .

@{
var data = "";
var count = 1;
foreach (var item in ViewModel.Data)
{
    data += count.ToString() + "," + item.Reading.ToString() + "\n";
    count++;
}

}

I want to pass this 'data' variable to a very tiny script in order to create a graph using Dygraph. This is what I currently have, and believe should work, it doesn't though.

<script type="text/javascript">
g = new Dygraph(document.getElementById("readingGraph"), @data,{ labels: ["Sample Number", "Reading"] });
</script>

I know the script itself will work. If I hard-code some CSV values into a string where @data is, the graph pops up and everything is fine. With the @data there, the graph doesn't load and the script won't run at all (I placed an alert('hi') in there to see if it would pop-up, it didn't).

In addition, I know the string is being 'built' correctly because I have set break points in Visual Studio and checked.

Any help at all would be great guys. Thanks in advance.

解决方案

I found the issue. A JS literal string cannot span multiple lines (which is how I was feeding it information via the C# created 'data' variable).

I had to add an '@' in the C# code in order to turn it into a string literal. . .

foreach (var item in CompletePreview.BatteryData)
{
    data += count.ToString() + "," + item.Voltage + @"\n"; //note the new @ in front of "\n"
    count++;
}

The script had to be altered to replace the literal '\n' with one that would break it up in JS.

g = new Dygraph(document.getElementById("voltGraph"), jsgraphData.replace('\n','\n'), { labels: ["Sample Number", "Voltage"] });
alert('graphs should follow');

Thanks everybody for the help. I appreciate it greatly.

这篇关于使用MVC/Razor传递C#变量以与Dygraph一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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