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