The ASP. NET MVC controller receives the JSON object or array data sent by the Ajax post method

Source: Internet
Author: User

The purpose of this example is to illustrate how one of my controllers receives the JSON object or array information from the AJAX submission (POST), and feels there should be a better way to welcome the valuable advice. json.stringify (jsonobj) does not support IE8 the following browser front-end page code is as follows:

<Scripttype= "Text/javascript"src= "~/scripts/jquery-1.11.3.js"></Script> <formID= "FM">     <Div>         <inputID= "BTN"type= "button"value= "Submit"onclick= "Savetest ()" />     </Div> </form> <Script>    functionsavetest () {varJsonobj= []; Jsonobj.push ({"ID":1,"name":"123"}); Jsonobj.push ({"ID":2,"name":"234"}); Jsonobj.push ({"ID": 3, "name": "345" }); $.ajax ({URL:"/test/save", type:"Post", DataType:"JSON", Async:false, Data:JSON.stringify (jsonobj), success:function(data) {}, Error:function(XMLHttpRequest, Textstatus, Errorthrown) {alert (xmlhttprequest.status);                 alert (xmlhttprequest.readystate);             alert (textstatus);     }         }); }</Script>

The Controller->action code is as follows (you need to reference the System.IO and System.Web.Script.Serialization namespaces):

[HttpPost] PublicActionResult Save () {varSR =NewStreamReader (Request.inputstream); varstream =Sr.             ReadToEnd (); JavaScriptSerializer JS=NewJavaScriptSerializer (); varList = js. Deserialize<list<models.selectlist>>(stream); if(list. Any ()) {foreach(varIteminchlist) {                                      }             }             returnView (); }

SelectList object class (class can be added here [Serializable] or no, because there is no direct transfer or storage of SelectList objects, so I do not add)

 Public Partial class SelectList     {         publicintgetset;}            Public string Get Set ; }            Public string Get Set ; }     }

The general idea is to convert a JSON object or array into a string, which is passed to the server by the front end, request is received, and then deserialized into the entity object using JSON. Tried the action to add a string argument directly, but after entering the action, you cannot receive a JSON string that passes over. Of course, you can also save the JSON string directly in the hidden domain, and the form is submitted to the server for access.

Transferred from: http://www.echojb.com/ajax/2016/11/20/264249.html

The ASP. NET MVC controller receives the JSON object or array data sent by the Ajax post method

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.