Reputation: 13895
I have a GetUserById function that returns a User. The User has a Roles property which is an object list of Roles.
Role class looks like this:
public int Id { get; set; }
public int Name { get; set; }
In my MVC UI I have a UserViewModel that has a property List RoleIds since all the view needs to now about is the Role Ids to select the appropriate values in my MultiSelect.
When I use AutoMapper to convert from User to UserViewModel how can I specify that I want to take all of the Ids from User.Roles and put them in UserViewModel.RoleIds in the mapping settings.
Upvotes: 0
Views: 288
Reputation: 13895
Was able to achieve this with the following:
CreateMap<User, UserViewModel>()
.ForMember(vm => vm.RoleIds, opt => opt.MapFrom(u => u.Roles.Select(r => r.Id)));
Upvotes: 1