2011-12-13 127 views
0

是否有可能將UserViewModel自動映射到用戶?asp.net mvc automapping查看模型到域模型

public class User 
{ 
    [Key] 
    public virtual int UserId { get; set; } 

    public virtual string Name { get; set; } 
    public virtual string Surname { get; set; } 
    [Required] 
    public virtual string Username { get; set; } 
    [Required] 
    public virtual string Password { get; set; } 

    public virtual ICollection<Role> Roles { get; set; } 
} 


public class UserViewModel 
{ 
    public User User { get; set; } 

    public List<Role> AvailableRoles { get; set; } 
    public List<Role> AssignedRoles { get; set; } 

    public int[] AvailableSelected { get; set; } 
    public int[] AssignedSelected { get; set; } 
    public string SavedAssigned { get; set; } 
} 

AutoMapper.Mapper.CreateMap<UserViewModel, User>(); 
AutoMapper.Mapper.Map(model, user); 

我試過這個,但它不起作用。我需要它用於用戶控制器中的「編輯」操作方法,以將更改保存到數據庫。

回答

0

你試過

AutoMapper.Mapper.CreateMap<UserViewModel, User>()  
    .ForMember(dest => dest, opt => opt.MapFrom(src => src.User));