【发布时间】:2013-06-12 20:58:09
【问题描述】:
我想让一些绑定在我的 mvc 应用程序中工作。我发现嵌套属性不会被 asp.net mvc 的 RC1 版本中的默认模型绑定器自动绑定。我有以下类结构:
public class Contact{
public int Id { get; set; }
public Name Name { get; set; }
public string Email { get; set; }
}
其中Name定义为:
public class Name{
public string Forename { get; set; }
public string Surname { get; set; }
}
我的观点是这样定义的:
using(Html.BeginForm()){
Html.Textbox("Name.Forename", Model.Name.Forename);
Html.Textbox("Name.Surname", Model.Name.Surname);
Html.Textbox("Email", Model.Email);
Html.SubmitButton("save", "Save");
}
我的控制器动作定义为:
public ActionResult Save(int id, FormCollection submittedValues){
Contact contact = get contact from database;
UpdateModel(contact, submittedValues.ToValueProvider());
//at this point the Name property has not been successfully populated using the default model binder!!!
}
Email 属性已成功绑定,但 Name.Forename 或 Name.Surname 属性未绑定。谁能告诉这是否应该使用默认模型绑定器工作,我做错了什么,或者它是否不起作用,我需要滚动自己的代码来绑定模型对象上的嵌套属性?
【问题讨论】:
标签: asp.net-mvc