Use modelbinding in ASP. net mvc to construct an array, list, collection, and Dictionary

Source: Internet
Author: User

In Asp.net MVC, we can pass parameters in specific formats in HTML forms to construct some set types through model binder.

Method 1

For example, this method

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

Add these input elements to the form.

<Input type = "text" name = "People [0]. firstname "value =" shen "/> <input type =" text "name =" People [0]. lastname "value =" "/> <input type =" text "name =" People [1]. firstname "value =" depressing "/> <input type =" text "name =" People [1]. lastname "value =" PP "/> <input type =" text "name =" People [3]. firstname "value =" weight "/> <input type =" text "name =" People [3]. lastname "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 like this.

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 we will followParametername [Index]. propertynameTo parse attributes. 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

Let's take a look at the following situation.

The method signature is as follows:

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

HTML is constructed like this

<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 "/>

We will get such a set of 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 resolution method is to findParametername [Index]. KeyThis structure and the way to parse value is to findParametername [Index]. ValueSuch a structure. If the key or value is of a complex type (for example, the information type in the preceding example ),Parametername [Index]. KeyOrParametername [Index]. ValueWill be treated as a prefix (or can be understood as a type) and. PropertynameIs 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.

Construct ienumerable <t>, icollection <t>, ilist <t>, t [], collection <t>,AndList <t>The first syntax is required for this type of set.
While buildingIdictionary <tkey, tvalue>AndDictionary <tkey, tvalue>This type of set requires the second syntax.

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

References:

Http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

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.