I am looking for decent template advice for selecting a list of dropdowns and saving the selection with POCO EF.
I have an IEnumerable<Country> list in my view model, where Country is POCO loaded via EF. The view model has an Address property that accepts the current or user-selected value of the Country property. In the view, I show them through Html.DropdownListFor (), this way:
Html.DropDownListFor(model => model.Address.Country.Id, new SelectList(Model.Countries,"Id","Name",model.Address.Country.Id)
So far, so good, and it all works in reverse with the default ModelBinder, providing me with a view model with Address.Country populated. However, Address.Country, of course, is only populated with an Id field with a default binding.
An attempt to send an address update back to DB via EF is discarded, since it is considered as a new object that does not have a full loaded graph of the object, but only a set of identifiers.
Now I can fix this by loading the entire Country object from db into the Address.Country property by postback before saving based on the selected identifier. But this seems like very hard work for anything other than a simple graphic object.
The most βelegantβ solution that I could think of would be a mandatory binder for the country, but then it would require the Binder model to learn about the repository to retrieve the full EF object, which does not seem right to me. I must also repeat this for all other objects used in decay lists.
Hope this makes sense, and any feedback on how others do it will be appreciated.