简体   繁体   中英

Read C# dictionary to Javascript object

How can I read C# dictionary to Javascript in MVC3 without Razor? I can do this fine with newer MVC with

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

But my older project does not recognize 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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