简体   繁体   English

将C#字典读取为Javascript对象

[英]Read C# dictionary to Javascript object

How can I read C# dictionary to Javascript in MVC3 without Razor? 在没有Razor的情况下,如何在MVC3中阅读C#字典到Javascript? I can do this fine with newer MVC with 我可以用更新的MVC做到这一点

var texts = @Html.Raw(JsonConvert.SerializeObject(Model.TextDict));

But my older project does not recognize Jsoncovert. 但是我的旧项目无法识别Jsoncovert。 I tried with: 我尝试了:

var texts = <% Html.Raw(Json.Encode(Model.TextDict)); %>

But here the texts is undefined. 但是这里的文本是不确定的。

That because <% ... %> simply execute the code inside and doesn't return anything. 那是因为<% ... %>只是在内部执行代码,并且不返回任何内容。 You should use <%= ... %> instead. 您应该改用<%= ... %> Try this: 尝试这个:

var texts = <%= Html.Raw(Json.Encode(Model.TextDict)); %>

采用

var texts = @Html.Raw(JsonConvert.SerializeObject(Model.TextDict));

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

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