Alex Rivera | Logout

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

Asked 2009-05-20T11:59:10.257
10

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

Edit
Report

1 Answer

1

I've been struggling with this same type of scenario and eventually came to realize that the underlying problem is that the MVC default model binder does not seem to work on EntitySet<T> fields, only List<T> fields. I did however find a simple workaround that seems acceptable. In my case, I have a Company entity that has one to many relationship to Contacts (my Linq-to-Sql EntitySet).

Since it seems that when I change my code from EntitySet<Contact> to List<Contact>, the MVC default model binder starts working as expected (even though the LTS isn't now), I figured I would provide an alternate, "aliased" property to MVC that is of type List<Contact>, and sure enough, this seems to work.

In my Company entity class:


// This is what LINQ-to-SQL will use:
private EntitySet<Contact> _Contacts = new EntitySet<Contact>();
[Association(Storage="_Contacts", OtherKey="CompanyID", ThisKey="ID")]
public EntitySet<Contact> Contacts
{
    get { return _Contacts; }
    set { _Contacts.Assign(value); }
}

// This is what MVC default model binder (and my View) will use:
public List<Contact> MvcContacts
{
    get { return _Contacts.ToList<Contact>(); }
    set { _Contacts.AddRange(value); }
}

So now, in my View, I have the following:

<label>First Name*
    <%= Html.TextBox("Company.MvcContacts[" + i + "].FirstName") %>
</label>
<label>Last Name*
    <%= Html.TextBox("Company.MvcContacts[" + i + "].LastName") %>
</label>

Seems to work like a charm!

Best of luck! -Mike

answered 2009-05-22T03:34:21.470

Your Answer