Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net-mvc

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 correctly, but it just does not populate the collection within the model when I post the form to the server.

I have a piece of data which is found in the class hierarchy thus:

person.PersonDetails.ContactInformation[0].Data;

This class structure is created by LinqToSQL, and ContactInformation is of type EntitySet<ContactData>. To create the view I pass the following:

return View(person);

and within the view I have a form which contains a single text box with a name associated to the above mentioned field:

<%= Html.TextBox("person.PersonDetails.ContactInformation[0].Data")%>

The post method within my controller is then as follows:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create (Person person)
{
  //Do stuff to validate and add to the database 
}

It is at this point where I get lost as person.PersonDetails.ContactInformation.Count() ==0. So the ModelBinder has created a ContactInformation object but not populated it with the object which it should hold (i.e ContactData) at index 0.

My question is two fold: 1. Have I taken the correct approach.. i.e. should this work? 2. Any ideas as to why it might be failing to populate the ContactInformation object?

Many thanks, Richard

like image 453
Richbits Avatar asked May 20 '09 11:05

Richbits


2 Answers

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.

like image 179
tvanfosson Avatar answered Sep 30 '22 12:09

tvanfosson


If a property is null, then the model binder other could not find it or could not find values in the submitted form necessary to make an instance of the type of the property. For example, if the property has a non-nullable ID and your form does not contain any data for that ID , the model binder will leave the property as null since it cannot make a new instance of the type without knowing the ID.

In other words, to diagnose this problem you must carefully compare the data in the submitted form (this is easy to see with Firebug or Fiddler) with the structure of the object you are expecting the model binder to populate. If any required fields are missing, or if the values are submitted in such a way that they cannot be converted to the type of a required field, then the entire object will be left null.

like image 43
Craig Stuntz Avatar answered Sep 30 '22 14:09

Craig Stuntz