Example of passing array parameters in ASP. net mvc in post mode, asp. netmvc

Source: Internet
Author: User

Example of passing array parameters in ASP. net mvc in post mode, asp. netmvc

Recently, I used ASP. NET MVC to pass array parameters in post mode and record them for reference.

1. Prepare the parameter object

In this example, we will pass two array parameters: A String Array and a custom object array. This custom object UserInfo is defined as follows:

    public class UserInfo    {        public int UserId { get; set; }        public string UserName { get; set; }    }
Ii. Background code

The background Action code is as follows:

[HttpPost] public ActionResult TestPost (List <string> a, UserInfo [] B) {var result = new {IsSuccess = true, ErrMsg = "You are wrong "}; return Json (result, JsonRequestBehavior. allowGet );}

The List or array method is the same for front-end code writing.

3. Front-end code

The front-end jquery call code is as follows:

<Html> Iv. Summary

In ASP. net mvc, the key to passing array parameters in post mode is the json object writing method. Here we will summarize:

First define an empty object: var data = {};

For simple types such as List <string> and List <int>, data ["a [0]"] = "dd "; data ["a [1]"] = "d2 ";....

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

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.