Jquery Ajax passes array parameter values to the service side

Source: Internet
Author: User
Tags javascript array

When using MVC, it is sometimes necessary to pass an array as a parameter value when sending a POST request to the server side

Let's take a look at the action below using the example

[HttpPost]  Public ActionResult Test (list<string> model) {    return Json (null, Jsonrequestbehavior.allowget);}

Type one, construct the form element, and then call the Serialize () method to get the constructor argument string

@{Layout = null;}<!DOCTYPE HTML><HTML><Head>    <Metaname= "Viewport"content= "Width=device-width" />    <title>Test</title></Head><Body>    <Div>        <inputtype= "button"ID= "Btnajax"value= "Send Request" />    </Div>    <Scriptsrc= "~/scripts/jquery-1.10.2.min.js"></Script>    <Scripttype= "Text/javascript">        vartmp= '<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 parameters monitored are as follows

Method Two: Use a JavaScript object as a parameter value, the parameter name is the parameter name corresponding to the action method, the parameter value is a JavaScript array

@{Layout = null;}<!DOCTYPE HTML><HTML><Head>    <Metaname= "Viewport"content= "Width=device-width" />    <title>Test</title></Head><Body>    <Div>        <inputtype= "button"ID= "Btnajax"value= "Send Request" />    </Div>    <Scriptsrc= "~/scripts/jquery-1.10.2.min.js"></Script>    <Scripttype= "Text/javascript">        //var tmp = ' <input type= ' hidden "name=" model "value=" 1 "/><input type=" hidden "name=" model "value=" 2 "/> ';         varArray= ["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 three, using JSON as the parameter request, at this time Ajax needs to declare Content-type as Application/json

@{Layout = null;}<!DOCTYPE HTML><HTML><Head>    <Metaname= "Viewport"content= "Width=device-width" />    <title>Test</title></Head><Body>    <Div>        <inputtype= "button"ID= "Btnajax"value= "Send Request" />    </Div>    <Scriptsrc= "~/scripts/jquery-1.10.2.min.js"></Script>    <Scripttype= "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

Jquery Ajax passes array parameter values to the service side

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.