Example of passing array parameters as post in ASP.

Source: Internet
Author: User

The recent use of the post-pass array parameter in ASP. NET MVC is documented for reference.

First, prepare the Parameter object

In this case, I'll pass two array parameters: an array of strings, a custom array of objects. The custom object userinfo is defined as follows:

     Public class UserInfo    {        publicintgetset;}          Public string Get Set ; }    }
Second, backstage code

The background action code is as follows:

        [HttpPost]        public actionresult testpost (list<string > A, userinfo[] b)        {            varnewtrue" you are wrong "};             return Json (result, jsonrequestbehavior.allowget);        }

Where the list or array method is the same for the front-end code.

Third, the front-end code

The front-end jquery calling code is as follows:

        $(function() {            $(' #btnTest '). Click (function() {                vardata = {}; data["A[0]" [] = "Test"; data["a[1]" = "Just"; data[[B[0]. UserId "] = 1; data[[B[0]. UserName "] =" Zhang "; data[[B[1]. UserName "] =" Sir Huang "; $.post (' @Url. Action ("Testpost") ', data,function(res) {alert (' Result: ' + res. Issuccess + ', Error: ' +Res.                ERRMSG);            });        });    }); </script>Iv. Summary

The key to passing array parameters in ASP. Post is the way JSON objects are written, and here's a summary:

Define the Empty object first: var data = {};

For list<string>, list<int> and other simple types, data["a[0" "] =" DD "; data["a[1]" = "D2"; ....

For complex types, such as List<userinfo>, userinfo[],data["b[0". UserId "] = 1; ....

Example of passing array parameters as post in ASP.

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.