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
public ActionResult Action(Contact foo){}
2,基于名称的简单类型数组绑定(名称必须相同)
//数据源范例
Public ActionResult ActionMethod(string[] foo){}//action的参数名“foo”,必须与表单中的name的值匹配
3,基于索引的对象数组绑定(索引必须从0开始,且连续,不连续会导致后面的绑定丢失)
//请求表单范例
public ActionResult Index(Contact[] contacts){}
4,集合(除数组和字典之外的所有实现IEnumerable<T>接口的类型)
//请求表单范例
//集合范例public ActionResult Action(IEnumerablecontacts){
foreach (var contact in contacts){}
}
5,字典(实现了接口IDictionary<TKey,TValue>的类型)
//请求表单范例,注意Key的值不能重复,且索引要连续
public ActionResult Action(IDictionarycontacts) { foreach (string key in contacts.Keys) { Contact contact = contacts[key]; } }
6,混合应用,属性是数组
public class QuizAnswer { public int QuizId { get; set; } public string[] QuizAnswerArray { get; set; }//答案为数组 }
//控制器代码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 IListJobList{ get; set; }//这里是个集合 }//职位 public class Job { public string Title{ get; set; } public string Detail{ get; set; } }
//注意集合的前缀为List
//controllerPublic ActionResult ActionMethod(Company company){ foreach(var job in company.JobList){}}
参考http://www.cnblogs.com/artech/archive/2012/05/30/default-model-binding-02.html