MVC复杂类型的模型绑定

1,属性为引用类型(非集合,非数组)

//模型1
 public class Contact
  {
      public string Name { get; set; }
      public string PhoneNo { get; set; }
      public string EmailAddress { get; set; }
      public Address MyAddress { 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; }
  }
//请求表单范例,name属性,具有显著的层次结构,注意子属性前缀用的是属性名MyAddress
<input name="Name" type="text" ... />
<input name="PhoneNo" type="text" ... />
<input name="EmailAddress" type="text" ... />
<input name="MyAddress.Province" type="text" ... />
<input name="MyAddress.City" type="text" ... />
<input name="MyAddress.District" type="text" ... />
<input name="MyAddress.Street" type="text"... />
public ActionResult Action(Contact foo){}

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){}//action的参数名“foo”,必须与表单中的name的值匹配

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

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

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 (var contact in contacts){}
}

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

//请求表单范例,注意Key的值不能重复,且索引要连续
<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="[1].Key" 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)
         {
             Contact contact = contacts[key];
         }
     }

 6,混合应用,属性是数组

public class QuizAnswer
  {
      public int QuizId { get; set; }
      public string[] QuizAnswerArray { get; set; }//答案为数组
     
  }
<!--第一题,单选-->
<input  name="[0].QuizId" type="hidden" value="101" />
<input  name="[0].QuizAnswerArray" type="radiobox" value="A" /> 
<input  name="[0].QuizAnswerArray" type="radiobox" value="B" /> 
<input  name="[0].QuizAnswerArray" type="radiobox" value="C" /> 
<input  name="[0].QuizAnswerArray" type="radiobox" value="D" /> 

<!--第二题,多选-->
<input  name="[1].QuizId" type="hidden" value="102" />
<input  name="[1].QuizAnswerArray" type="checkbox" value="A" /> 
<input  name="[1].QuizAnswerArray" type="checkbox" value="B" /> 
<input  name="[1].QuizAnswerArray" type="checkbox" value="C" /> 
<input  name="[1].QuizAnswerArray" type="checkbox" value="D" />
//控制器代码
public ActionResult SubmitAnswer(QuizAnswer[] answerArray)
{
foreach (var answer in answerArray)
            {  
            }
}

 7,混合应用,属性是集合

模型:属性是集合的情况

//公司
public class Company
  {
      public string Name { get; set; }
      public string City { get; set; }
      public IList<Job> JobList{ get; set; }//这里是个集合
  }
//职位
  public class Job
  {
      public string Title{ get; set; }
      public string Detail{ get; set; }
  }
//注意集合的前缀为List
<input name="Name" type="text" ... />
<input name="City" type="text" ... />

<input name="JobList[0].Title" type="text" ... />
<input name="JobList[0].Detail" type="text" ... />

<input name="JobList[1].Title" type="text" ... />
<input name="JobList[1].Detail" type="text" ... />

<input name="JobList[2].Title" type="text" ... />
<input name="JobList[2].Detail" type="text" ... />
//controller
Public ActionResult ActionMethod(Company company)
{
      foreach(var job in company.JobList){}
}

参考http://www.cnblogs.com/artech/archive/2012/05/30/default-model-binding-02.html

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