Use View.bag to save JSON strings in MVC. Use in JS without Ajax request ....

Source: Internet
Author: User

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 &quot;

With @html. Raw () Let it not transcode;

Use View.bag to save JSON strings in MVC. Use in JS without Ajax request ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.