When using MVC, sometimes you need to pass an array as a parameter value when sending a POST request to the server. The following is an example code to introduce to you the method of jQuery Ajax passing array parameter values to the server. Let's take a look. I hope it can help. to everyone.
The following examples are used to illustrate, first look at Action
[HttpPost] public ActionResult Test(List<string> model) { return Json(null, JsonRequestBehavior.AllowGet); }
Method 1, construct the form element, and then call the serialize() method to get the construction parameter string
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Test</title> </head> <body> <p> <input type="button" id="btnAjax" value="发送请求" /> </p> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script type="text/javascript"> var tmp = '<input type="hidden" name="model" value="1" /><input type="hidden" name="model" value="2" />'; $(function () { $("#btnAjax").click(function () { $.ajax({ url: '@Url.Action("Test")', type: 'POST', data: $(tmp).serialize(), success: function (json) { console.log(json); } }); }); }); </script> </body> </html>
Debug mode monitoring parameters, when the button is clicked, the monitored parameters are as follows
Method 2: Use JavaScript objects as parameters to pass values. The parameter name is the parameter name corresponding to the Action method, and the parameter value is a JavaScript array
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Test</title> </head> <body> <p> <input type="button" id="btnAjax" value="发送请求" /> </p> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script type="text/javascript"> //var tmp = '<input type="hidden" name="model" value="1" /><input type="hidden" name="model" value="2" />'; var array = ["abc","123"]; $(function () { $("#btnAjax").click(function () { $.ajax({ url: '@Url.Action("Test")', type: 'POST', data: { model:array }, success: function (json) { console.log(json); } }); }); }); </script> </body> </html>
Method 3, use Json as parameter request, at this time Ajax needs to declare Content-Type as application/json
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Test</title> </head> <body> <p> <input type="button" id="btnAjax" value="发送请求" /> </p> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script type="text/javascript"> //var tmp = '<input type="hidden" name="model" value="1" /><input type="hidden" name="model" value="2" />'; //var array = ["abc","123"]; $(function () { $("#btnAjax").click(function () { $.ajax({ url: '@Url.Action("Test")', type: 'POST', contentType:'application/json;charset=utf-8', data: JSON.stringify({ model:["hello","welcome"] }), success: function (json) { console.log(json); } }); }); }); </script> </body> </html>
The above example uses ASP.NET MVC 5
Related recommendations:
How to use php to pass arrays to js scripts
jquery ajax fails to pass arrays to the background
The above is the detailed content of Detailed explanation of Ajax passing array parameter value to the server. For more information, please follow other related articles on the PHP Chinese website!