ASP.NET MVC - Binding a Child Entity to the Model
- by Nathan Taylor
This one seems painfully obvious to me, but for some reason I can't get it working the way I want it to. Perhaps it isn't possible the way I am doing it, but that seems unlikely. This question may be somewhat related: http://stackoverflow.com/questions/1274855/asp-net-mvc-model-binding-related-entities-on-same-page.
I have an EditorTemplate to edit an entity with multiple related entity references. When the editor is rendered the user is given a drop down list to select related entities from, with the drop down list returning an ID as its value.
<%=Html.DropDownListFor(m => m.Entity.ID)%>
When the request is sent the form value is named as expected: "Entity.ID", however my strongly typed Model defined as an action parameter doesn't have Entity.ID populated with the value passed in the request.
public ActionResult AddEntity(EntityWithChildEntities entityWithChildEntities) { }
I tried fiddling around with the Bind() attribute and specified Bind(Include = "Entity.ID") on the entityWithChildEntities, but that doesn't seem to work. I also tried Bind(Include = "Entity"), but that resulted in the ModelBinder attempting to bind a full "Entity" definition (not surprisingly).
Is there any way to get the default model binder to fill the child entity ID or will I need to add action parameters for each child entity's ID and then manually copy the values into the model definition?