1

I have a parent and child domain object being mapped to a single ViewModel object. E.G.

class Parent
{
    public int ID {get;set;}
    public string Name {get;set;}
    public Child Child {get;set;}
}

class Child
{
    public int ID {get; set;}
    public string Name {get; set;}
}

class ParentChildVM
{
    public int ID {get;set;}
    public string Name {get;set;}
    public int ChildID {get;set;}
    public string ChildName {get; set;}
}

The types are mapped in static configuration file and it just uses the built in naming conventions to map the properties and achieve the flattening.

I'm using this ParentChildVM with an ASP.NET MVC view that puts ID and Name in form inputs, but only displays ChildID and ChildName. Since the Child's properties are not in form fields, they are not posted back to the server upon saving and are empty in the ParentChildVM that is reconstructed from the posted values.

So I want to remap the Child domain object to my ParentChildVM to fill in the missing properties. But I don't want to map the Parent again because it would overwrite the edited values. Is there any way to map a given Child instance into my existing ParentChildVM with AutoMapper (v6.1.1.0)?

EDIT:

I guess I know I could add a mapping from Child -> ParentChildVM and then use .ForMember to tell it how to map ChildID and ChildName, but in reality there are a lot more properties so what I'm asking is: is there a way to do it and still take advantage of the naming-convention-based mapping? I.E. keep using the "Auto" part of AutoMapper.

xr280xr
  • 12,621
  • 7
  • 81
  • 125

0 Answers0