简体   繁体   English

通过Razor(MVC4)将C#字典对象传递给JavaScript

[英]passing C# dictionary object to JavaScript through Razor (MVC4)

I'm passing dictionary to my view and I'm using razor to pass it to JS. 我正在将字典传递给我的视图,并且正在使用剃刀将其传递给JS。

My Code : 我的代码:

var data = []; 
@foreach(var tmp in Model){  
<text> data.push([ @tmp.Key,@tmp.Value]); </text>
}

My problem is that the code after it won't work. 我的问题是代码无法使用。 What am I missing out? 我错过了什么?

You might need the quotes around the values. 您可能需要在值两边加上引号。

<text>data.push(["@tmp.Key","@tmp.Value"]);</text>

Take a look at the actual source (right click view source), and see if it's valid javascript or not. 查看实际的源代码(右键单击查看源代码),然后查看它是否有效的javascript。

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

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