Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving Dropdown list selection with Entity Framework in ASP.NET MVC solution
    primarykey
    data
    text
    <p>I'm looking for advice on a decent pattern for dropdown list selection and persistence of the selection with POCO EF please.</p> <p>I have a list of <code>IEnumerable&lt;Country&gt;</code> in my view model where Country is a POCO loaded via EF. There is an Address property on the view model that takes the current or user selected value on it's Country property. Within the view I display these via a Html.DropdownListFor() thus:</p> <pre><code>Html.DropDownListFor(model =&gt; model.Address.Country.Id, new SelectList(Model.Countries,"Id","Name",model.Address.Country.Id) </code></pre> <p>So far so good and it all works on postback with the default ModelBinder providing me with a view model with the Address.Country populated. However Address.Country is of course only populated with the Id field with default model binding.</p> <p>Trying to send the Address update back to the DB through EF blows up as this is seen as a new object which doesn't have it's full object graph loaded, only the Id set.</p> <p>Now I can fix this by loading the full Country object from the db into the Address.Country property on postback before saving based on the selected Id. But this seems like a lot of hard work for anything beyond a simple object graph.</p> <p>The most "elegant" solution I could think of would be a custom model binder for Country but then that would require the Model Binder to know about the repository for retrieving the full EF object which doesn't seem right to me. I'd also have to repeat this for all other Entities used in Dropdown lists.</p> <p>Hope this makes sense and any feedback on how others are doing this would be appreciated. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload