In my NHIbernate (Database Model) I have this :
public class Pers {
public int Id{ get; set ;}
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public string City{ get; set ;}
public int Age{ get; set ;}
public Role Role{ get; set ;}
}
I have some dropwon (Database mode) :
public class Role {
public int Id{ get; set ;}
public string NL{ get; set ;}
public string FR{ get; set ;}
}
In my view I'd like use the dropdown and display some record (not all, in my real class there are much more properties) of Pers
. I created a Dto class for Pers with the fields I need :
public class PersDto {
public int Id{ get; set ;}
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
public class RoleDto {
public int Id{ get; set ;}
public string NL{ get; set ;}
public string FR{ get; set ;}
}
In the controller :
Mapper.CreateMap<Role, RoleDto>();
myModel.RoleDto = Mapper.Map<Role, RoleDto>(roleListFromDB);
Mapper.CreateMap<Pers, PersDto>();
myModel.PersDto = Mapper.Map<Pers, PersDto>(persFromDB);
public class MyModel{
public PersDto PersDto{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
Is it the right way ? Or it's better to do this with creating a PersDto
:
public class MyModel{
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
Is it possible with automapper to copy only some fields and not all ?
Thanks,