I'm using MVC 2.0 in an ASP.NET application using NHibernate.
I have a working View, Controller and data access layer using NHibernate that is able to display and save an entity with a relationship to another mapped entity:
Person -- > Location
It's using the HTML helper HTML.DropDownListFor() to display a list of all Locations. The user is able to select one of the Locations from the list, and press save.
The default model binder correctly sets the value of the Location on the Person entity being saved. This location is an nhibernate mapped entity, and is instantiated and has the id value that was selected in the dropdown list. Obviously, since the dropdown list that holds locations only has the ids of the locations, the rest of the values for the location are null. This is OK. I am only trying to save the Person with a reference to an existing location.
So, here comes the complication. We have a need to change the relationship between the two entities. Now the Person can have a reference to many locations.
Person.Locations will be an IList
My question is, how do you get the default model binder to take selections from a multiselect dropdown and populate an IList.
I've managed to save collections of entities in the past using the syntax [index].PropertyName as explaing by Phil Haacked .... http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx.
The issue here is that I have only a dropdown list, and it will post back to the modelbinder a repeating key with different values:
Person.Location.Id: 2
Person.Location.Id: 4
Person.Location.Id: 5
This, unfortunately, doesn't work. the Location list keeps coming back Null.
Our UI guy is using a slick JQuery pluggin to display the items in the select list, so I'd rather not have to use a different UI.
Any ideas?