且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在asp.net mvc的嵌套属性的模型绑定

更新时间:2023-02-25 18:31:27

我认为这个问题是由于对属性名称preFIX。我认为你需要更新它的两款车型,并指定为第二个的preFIX。请注意,我已经删除了的FormCollection 从参数和使用的UpdateModel 的依赖于签名内置价值提供者和指定属性的白名单来考虑。

I think the problem is due to the Name prefix on the properties. I think you'll need to update it as two models and specify the prefix for the second one. Note that I've removed the FormCollection from the parameters and used the signature of UpdateModel that relies on the built-in value provider and specifies a whitelist of properties to consider.

 public ActionResult Save( int id )
 {
     Contact contact = db.Contacts.SingleOrDefault(c => c.Id == id);

     UpdateModel(contact, new string[] { "Email" } );
     string[] whitelist = new string[] { "Forename", "Surname" };
     UpdateModel( contact.Name, "Name", whitelist );
 }