asp.net mvc将视图模型自动化到域模型

时间:2011-12-13 23:23:23

标签: asp.net-mvc entity-framework automapper

是否可以将UserViewModel自动化为用户?

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);

我试过这个,但是不行。我需要在用户控制器中使用我的编辑操作方法将更改保存到数据库。

1 个答案:

答案 0 :(得分:0)

你试过吗

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