ASP.Net MVC - model with collection not populating on postback

后端 未结 6 475
面向向阳花
面向向阳花 2020-12-01 19:08

I have an ASP.Net MVC application with a model which is several layers deep containing a collection.

I believe that the view to create the objects is all set up co

6条回答
  •  [愿得一人]
    2020-12-01 19:50

    I think that your model is too complex for the default model binder to work with. You could try using multiple parameters and binding them with prefixes:

    public ActionResult Create( 
        Person person,
        [Bind(Prefix="Person.PersonDetails")]
        PersonDetails details,
        [Bind(Prefix="Person.PersonDetails.ContactInformation")] 
        ContactInformation[] info )
    {
          person.PersonDetails = details;
          person.PersonDetails.ContactInformation = info;
    
          ...
    }
    

    Or you could develop your own custom model binder that would understand how to derive your complex model from the form inputs.

提交回复
热议问题