Use ModelBinding in Asp. Net MVC to construct Array, List, Collection, Dictionary, mvcmodelbinding

Source: Internet
Author: User

Use ModelBinding in Asp. Net MVC to construct Array, List, Collection, Dictionary, mvcmodelbinding

In asp.net mvc, we can pass parameters in specific formats in html forms to construct some set types through model binder.

Method 1

public ActionResult Infancy(Person[] people){    // ...  } 

Html form construction

<Input name = "people [0]. firstName "type =" text "value =" shen "/> <input name =" people [0]. lastName "type =" text "value =" "/> <input name =" people [1]. firstName "type =" text "value =" depressing "/> <input name =" people [1]. lastName "type =" text "value =" PP "/> <input name =" people [3]. firstName "type =" text "value =" weight "/> <input name =" people [3]. lastName "type =" text "value =" "/>

This is probably the case when it is submitted as an http post.

People % 5B0% 5D. firstName = God & people % 5B0% 5D. lastName = fish & people % 5B1% 5D. firstName = depressing & people % 5B1% 5D. lastName = PP & people % 5B3% 5D. firstName = heavy & people % 5B3% 5D. lastName = Code

Through model binder, we will get an Array set of such a people variable.

People [0]. firstName = "God" people [0]. lastName = "fish" people [1]. firstName = "depressing" people [1]. lastName = "PP" people [3]. firstName = "weight" people [3]. lastName = ""

This is the same as explicitly constructing the following set in the Code:

People = new Person [] {new Person () {FirstName = "God", LastName = "fish"}, new Person () {FirstName = "depressing ", lastName = "PP "}

Here, attributes are parsed according to the rules of parameterName [index]. PropertyName. The index must be a continuous positive integer starting with 0. In the preceding example, because there is no people [2], the "important code" will not be parsed.

Method 2

public ActionResult Infancy(IDictionary<string, Information> people){    // ...}  

Html form construction

<Input type = "text" name = "people [0]. key "value =" forever "/> <input type =" text "name =" people [0]. value. age "value =" 12 "/> <input type =" text "name =" people [0]. value. gender "value =" "/> <input type =" text "name =" people [1]. key "value =" depressing PP "/> <input type =" text "name =" people [1]. value. age "value =" 50 "/> <input type =" text "name =" people [1]. value. gender "value =" not easy to say "/>

Using model binder, we will get a set of people key values:

People [0]. key = "forever" people [0]. value. age = 12 people [0]. value. gender = "Gender" people [1]. key = "depressing PP" people [1]. value. age = 50 people [1]. value. gender = "not easy to say"

Constructed as we do in the Code:

Var people = new Dictionary <string, Information> () {"forever", new Information () {Age = 12, Gender = ""}}, {"depressing PP", new Information () {Age = 50, Gender = "not easy to say "}}};

Here, the key parsing method is to look for a structure like parameterName [index]. Key, and the value parsing method is to look for a structure like parameterName [index]. Value. If the key or value is of a complex type (for example, the Information type in the preceding example), parameterName [index]. key or parameterName [index]. value is treated as a prefix (or a type. propertyName is treated as a suffix (that is, an attribute ). The index must also be an uninterrupted positive integer starting with 0. Otherwise, parts that are disconnected will not be parsed.

The first syntax is required to construct IEnumerable <T>, ICollection <T>, IList <T>, T [], Collection <T>, and List <T>.
The second syntax is required to construct IDictionary <TKey, TValue> and Dictionary <TKey, TValue> collections.

If you do not like this method, you can define some binder in Asp.net mvc to process specific types. Or use FormCollection directly.

For more IT-related articles, welcome to my personal website: http://www.zuowenjun.cn/

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.