views:

57

answers:

0

Hi all,

I have been looking around for a nice working solution on how to correctly handle model binding with nested attributes. I have one model that has a list of other child models like below:

public class Organization : IEntity
{
    [ScaffoldColumn(false)]
    public int ID
    {
        get; 
        set;
    }

    [LocalizedDisplayName("Goals")]
    public virtual ICollection<OrganizationGoal> Goals
    {
        get;
        set;
    }
}

In the controller I try to update the data like this:

[HttpPost]
public ActionResult Edit(string organizationIdentifier, FormCollection values)
{
    var organization = organizationService.GetByIdentifier(organizationIdentifier);

    if (TryUpdateModel(organization))
    {
       organizationService.Save(organization);
       return RedirectToAction("Edit");
    }

    return View("Edit");
}

But the TryUpdateModel always return false and no validation messages are displayed in the UI. The UI is built using the standard MVC helper EditorFor.

What is the best practice of doing this? For a pretty normal scenario there is not that easy to find information.

Thanks!