开发者

AutoMapper problem with Dropdown

I´m started to work with AutoMapper today... But I´m having some problem with Dropdown model...

What I have so far :

User Model

public class User : Entity
{          
    public virtual string Name { get; set; }   
    public virtual string Email { get; set; }
    public virtual string Password { get; set; } 
    public virtual Role Role { get; set; }      
}

Role Model

public class Role : Entity
{          
    public virtual string Name { get; set; }   
}

UserUpdateViewModel

public class UserUpdateViewModel
{
    public int Id{get;set;}

    [Required(ErrorMessage = "Required.")]
    public virtual string Name { get; set; }

    [Required(ErrorMessage = "Required."), Email(ErrorMessage = "Email Invalid."), Remote("EmailExists", "User", ErrorMessage = "Email already in use.")]
    public virtual string Email { get; set; }

    [Required(ErrorMessage = "Required.")]
    public virtual string Password { get; set; }

    [Required(ErrorMessage = "Required")]
    public virtual string ConfirmPassword { get; set; }

    [Required(ErrorMessage = "Required.")]
    public int RoleId { get; set; }

    public IList<Role> Roles { get; set; }
}

UserController

public ActionResult Update(int id=-1)
{
      var _user = (_userRepository.Get(id));
      if (_user == null)
          return RedirectToAction("Index");

       Mapper.CreateMap<User, UserUpdateViewModel>();
       var viewModel = Mapper.Map<User, UserUpdateViewModel>(_user);

       viewModel.Roles = _roleRepository.GetAll();

       return View(viewModel);
}

[HttpPost, Transaction]
public ActionResult Update(UserViewModel user)
{
     if (ModelState.IsValid)
     {
         user.Password = _userService.GetPasswordHash(user.Password);

         Mapper.CreateMap<UserViewModel, User>();
         var model = Mapper.Map<UserViewModel, User>(user);  //model.Role = null

         _userRepository.SaveOrUpdate(model); //ERROR, because model.Role = null
         return Content("Ok");
     }
    开发者_JAVA百科 return Content("Erro").         
}

View Update

...
@Html.DropDownListFor(model => model.RoleId, new SelectList(Model.Roles, "Id", "Name"), "-- Select--", new { @class = "form radius" })   
...

Some considerations:

1 - I´m returning Content() because is all Ajax enabled using HTML 5 PushState etc etc

2 - In my Update(POST one) method, my model returned by Autommapper has Role = null

Why my Role returned by Automapper is null?

Is that the right way to work with AutoMapper? Any tip?

Thanks


The map is failing because you are trying to map a single Role directly to a collection of Roles. And a collection of Roles back to a single Role. You cant directly map between these as they are different types.

If you wanted to map a Role to a List then you could use a custom value resolver.

    Mapper.CreateMap<User , UserUpdateViewModel>()
    .ForMember(dest => dest.Roles, opt => opt.ResolveUsing<RoleToCollectionResolver>())


    Public class RoleToCollectionResolver: ValueResolver<User,IList<Role>>{ 
        Protected override IList<Role> ResolveCore(User source){
            var roleList = new List<Role>();
            roleList.Add(source.Role);
            Return roleList;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜