Sometimes we only need to use the JSON data, this time directly in the background to find out the JSON, and then use View.bag to the foreground, it will be very convenient. (It feels a little superfluous to ask for Ajax at the front desk.) )
On the Code
Background:
PublicActionResult Index () {List<Class1> str =NewList<class1>(); Str. ADD (NewClass1 () {name="Gao", age=" -"}); Str. ADD (NewClass1 () {Name ="Cong", age =" +" }); //Newtonsoft json.netVIEWBAG.G =Newtonsoft.Json.JsonConvert.SerializeObject (str); returnView (); }
View Code
Front desk
<script> var'@Html. Raw (VIEWBAG.G)'; alert (a); </script>
View Code
So you can happily use JS operation ...
Note: In JS, the special characters in the string are encoded by default, such as "will be converted to "
With @html. Raw () Let it not transcode;
Use View.bag to save JSON strings in MVC. Use in JS without Ajax request ....