Ajax submits a JSON string and. Net deserializes it into a model object.

Source: Internet
Author: User

1. Create a model object

 ///   <Summary>      ///  User entity class  ///   </Summary>      Public   Class  Usermodel {  ///   <Summary>          ///  User ID  ///   </Summary>          Public  Long Userid { Get ; Set  ;}  ///   <Summary>          ///  User display name  ///   </Summary>          Public   String Displayname { Get ; Set  ;} ///   <Summary>          ///  User Name  ///   </Summary>          Public   String Realname { Get ; Set  ;}} 

2. Create a JSON serialization help class

/* ===================================================== ========================================================== * Function description: jsonhelper * Creator: RES-WUZHOU * date of creation: 17:17:08 * =================================================== ========================================================== = */using system; using system. collections. generic; using system. LINQ; using system. text; using system. runtime. serialization. JSON; using system. io; using system. web. script. serialization; namespace common {// <summary> /// jsonhelper // </Summary> public class jsonhelper {// <summary> // JSON serialization // </ summary> /// <typeparam name = "T"> </typeparam> /// <Param name = "entity"> </param> /// <returns> </ returns> Public static string jsonserializer <t> (T entity) {var serializer = new javascriptserializer (); Return serializer. serialize (entity );} /// <summary> /// JSON deserialization /// </Summary> /// <typeparam name = "T"> </typeparam> /// <Param name = "jsonstring"> </param> // <returns> </returns> Public static t jsondeserialize <t> (string jsonstring) {var serializer = new javascriptserializer (); Return serializer. deserialize <t> (jsonstring );}}}

Note: manually reference system. Web. Script. serialization. dll

3. backend Cs method

 
Public actionresult add (string strusermodel) {usermodel = jsonhelper. jsondeserialize <usermodel> (strusermodel );//....}

4. Front-end Ajax submission

 
VaR usermodel ={}; usermodel. userid = 1; usermodel. displayname = "zhangsan"; usermodel. realname = "Zhang"; $. ajax ({URL: "/user/Add", type: "Post", data: {strusermodel: $. tojson (usermodel)}, success: function (JSON) {alert ('success');}, error: function () {alert ("system exception! ");}});

Note: jquery. js and jquery. JSON. js must be referenced on the page.

Related Article

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.