在Asp.Net MVC中使用ModelBinding構造Array、List、Collection以及Dictionary

來源:互聯網
上載者:User

在asp.net mvc中,我們可以在html表單中使用特定的格式傳遞參數,從而通過model binder構造一些集合類型。

第一種方式

比如這樣一個方法

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

並在表單中添加這些input元素

<input type="text" name="people[0].FirstName" value="神" /><input type="text" name="people[0].LastName" value="魚" /><input type="text" name="people[1].FirstName" value="鬱悶的" /><input type="text" name="people[1].LastName" value="PP" /><input type="text" name="people[3].FirstName" value="重" /><input type="text" name="people[3].LastName" value="典" />

當其作為一個HTTP POST被提交後的資料大概是這個樣子

people%5B0%5D.FirstName=神&people%5B0%5D.LastName=魚&people%5B1%5D.FirstName=鬱悶的&people%5B1%5D.LastName=PP&people%5B3%5D.FirstName=重&people%5B3%5D.LastName=典

那麼通過model binder我們將得到這樣的一個Array集合

people[0].FirstName = "神"  people[0].LastName = "魚"  people[1].FirstName = "鬱悶的"  people[1].LastName = "PP"  people[3].FirstName = "重"  people[3].LastName = "典" 

這樣就和我們在代碼中顯式的構建如下集合是一樣的

people = new Person[] {  new Person() { FirstName = "神", LastName = "魚" },  new Person() { FirstName = "鬱悶的", LastName = "PP" } 

這裡會按照parameterName[index].PropertyName的規則來解析屬性。其中,索引必須是連續的且以0開始的正整數。在上面的例子中由於沒有people[2],所以”重典”將不會被解析。

第二種方式

我們再來看下一種情況

方法簽名如下

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

Html這樣構造

<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="鬱悶的PP" /><input type="text" name="people[1].Value.Age" value="50" /><input type="text" name="people[1].Value.Gender" value="不好說" />

我們將會得到這樣的索引值集合

people[0].Key = "forever"  people[0].Value.Age = 12  people[0].Value.Gender = "純爺們"  people[1].Key = "鬱悶的PP"  people[1].Value.Age = 50people[1].Value.Gender = "不好說" 

如同我們在代碼中這樣構造

var people = new Dictionary<string, Information>() {    { "forever", new Information() { Age = 12, Gender = "純爺們" } },    { "鬱悶的PP", new Information() { Age = 50, Gender = "不好說" } }  };

這裡解析key的方式是尋找parameterName[index].Key這樣的結構、解析value的方式是尋找parameterName[index].Value這樣的結構。如果key或者value是複雜類型(如上面例子中的Information類型),則parameterName[index].KeyparameterName[index].value將被視為首碼(也可以理解為某個類型)而.PropertyName被視為尾碼(即某個屬性)。這裡的索引也要求必須是以0開始的不間斷的正整數。否則斷開以後的部分將不會被解析。

構建IEnumerable<T>, ICollection<T>, IList<T>, T[], Collection<T>, 以及 List<T>這類集合類型需要使用第一種文法。
而構建IDictionary<TKey, TValue>Dictionary<TKey, TValue>這類集合需要使用第二種文法。

如果你不喜歡這種方式,你完全可以在Asp.net mvc中自訂一些binder來處理特定的類型。或者直接使用FormCollection

參考資料:

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.