码迷,mamicode.com
首页 > Web开发 > 详细

MVC复杂类型的模型绑定

时间:2016-03-29 19:20:30      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

1,复杂类型

//模型范例
 public class Contact
  {
      public string Name { get; set; }
      public string PhoneNo { get; set; }
      public string EmailAddress { get; set; }
      public Address Address { get; set; }
  }
  public class Address
  {
      public string Province { get; set; }
      public string City { get; set; }
      public string District { get; set; }
      public string Street { get; set; }
  }
//请求表单范例
<input id="Name" name="Name" type="text" ... />
<input id="PhoneNo" name="PhoneNo" type="text" ... />
<input id="EmailAddress" name="EmailAddress" type="text" ... />
<input id="Address_Province" name="Address.Province" type="text" ... />
<input id="Address_City" name="Address.City" type="text" ... />
<input id="Address_District" name="Address.District" type="text" ... />
<input id="Address_Street" name="Address.Street" type="text"... />

 

public ActionResult Action(Contact foo, Contact bar){}

 

 

2,基于名称的数组绑定(名称必须相同

//数据源范例
 <input name="Foo" type="text" value="123"/>
 <input name="Foo" type="text" value="456" />
 <input name="Foo" type="text" value="789" />

 

Public ActionResult ActionMethod(string[] foo){}

 

 

3,基于索引的绑定(索引必须从0开始,且连续,不连续会导致后面的绑定失败)

//请求表单范例
<input  name="[0].Name" type="text" value="" .../> 
  
<input  name="[1].Name" type="text" value="" .../> 
 public ActionResult Index(string[] array){}

4,集合(除数组和字典之外的所有实现IEnumerable<T>接口的类型)

//请求表单范例
<input  name="[0].Name" type="text" value="" .../> 
<input  name="[0].PhoneNo" type="text" value="" .../> 
<input  name="[0].EmailAddress" type="text" value="" .../> 
  
<input  name="[1].Name" type="text" value="" .../> 
<input  name="[1].PhoneNo" type="text" value="" .../> 
<input  name="[1].EmailAddress" type="text" value="" .../> 

 

//范例
public ActionResult Action(IEnumerable<Contact> contacts){
foreach (Contact contact in contacts){}
}

5,字典(实现了接口IDictionary<TKey,TValue>的类型)

//请求表单范例
<input  name="[0].Key" type="text" value="Foo" .../> 
<input  name="[0].Value.Name" type="text" value="" .../> 
<input  name="[0].Value.PhoneNo" type="text" value="139" .../> 
<input  name="[0].Value.EmailAddress" type="text" value="a@z.com" .../> 
  
<input  name="[0].Name" type="text" value="Bar" .../> 
<input  name="[1].Value.Name" type="text" value="Bar" .../> 
<input  name="[1].Value.PhoneNo" type="text" value="138" .../> 
<input  name="[1].Value.EmailAddress" type="text" value="b@z.com" .../> 
public ActionResult Action(IDictionary<string, Contact> contacts)
     {
         foreach (string key  in contacts.Keys)
         {
             Response.Write(key + "<br/>");
             Contact contact = contacts[key];
             Response.Write(string.Format("&nbsp;&nbsp;&nbsp;&nbsp;{0}: {1}<br/>","Name", contact.Name));
             Response.Write(string.Format("&nbsp;&nbsp;&nbsp;&nbsp;{0}: {1}<br/>","PhoneNo", contact.PhoneNo));
             Response.Write(string.Format("&nbsp;&nbsp;&nbsp;&nbsp;{0}: {1}<br/><br/>", "EmailAddress", contact.EmailAddress));
         }
     }

 

MVC复杂类型的模型绑定

标签:

原文地址:http://www.cnblogs.com/imust2008/p/5334068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!