views:

220

answers:

2

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?

+1  A: 

Have you looked at AutoMapper? I agree that this shouldn't be required because the binding should work but...

I've had a little difficulty with using the Html.XYZFor helper and have reverted back to using the MVC 1.1 notation and it all works.

griegs
I actually haven't checked out AutoMapper yet. It's been on the to-do for a while.
Nathan Taylor
A: 

There's no DropDownListFor helper method that takes one parameter. The standard DropDownListFor method takes at least two parameters: the first is a lambda used to calculate the name of the select and the second is an IEnumerable<SelectListItem>:

<%= Html.DropDownListFor(m => m.Entity.ID, Model.EntitiesList) %>

Also how does the EntityWithChildEntities type looks like? It must be something like this:

public class EntityType
{
    public string ID { get; set; }
}

public class EntityWithChildEntities
{
    public EntityType Entity { get; set; }

    public IEnumerable<SelectListItem> EntitiesList
    {
        get 
        {
            return new[]
            {
                new SelectListItem { Value = "1", Text = "foo" },
                new SelectListItem { Value = "2", Text = "bar" },
            }
        }
    }
}
Darin Dimitrov
@Darin I appreciate the answer but my pseudo-code was not meant to be interpreted literally. From an implementation perspective everything is properly constructed; the problem is that despite receiving the correctly named values in the form post, the model's child entities are not being populated. In other words, the form field names and values are right, but the model output is not as expected.
Nathan Taylor