1

Well I am trying to map my user profile model to/from user profile viewmodel, but it will not work since they do not share all the data fields:

Model:

    public class UserProfile
{
    public virtual string Id { get; set; }
    public virtual string Interests { get; set; }
    public virtual string Biography { get; set; }
    public virtual string SmokingAttitude { get; set; }
    public virtual string DrinkingAttitude { get; set; }
    public virtual string Projects { get; set; }
    public virtual string Groups { get; set; }
}

ViewModel:

    public class UserProfileViewModel
{
    [Display(Name = "Interests")]
    public string Interests { get; set; }

    [Display(Name = "Biography/Comments")]
    public string Biography { get; set; }

    [Display(Name = "Attitude to Smoking")]
    public string SmokingAttitude { get; set; }

    [Display(Name = "Attitude to Drinking")]
    public string DrinkingAttitude { get; set; }

    [Display(Name = "Projects working on")]
    public string Projects { get; set; }

    [Display(Name = "Groups joined")]
    public string Groups { get; set; }


    public IEnumerable<SelectListItem> AttitudeList
    {
        get
        {
            return new[]{
                new SelectListItem {Value = "not specified", Text = "not specified"},
                new SelectListItem {Value = "very negative", Text = "very negative"},
                new SelectListItem {Value = "negative", Text = "negative"}, 
                new SelectListItem {Value = "compromisable",  Text = "compromisable"},
                new SelectListItem {Value = "neutral", Text = "neutral"},
                new SelectListItem {Value = "positive", Text = "positive"},
            };
        }
    }
}

As you can see, the ID attribute is stored in user profile model(as a key to user table), but not on user profile viewmodel. The user profile viewmodel though, has an additional field AttitudeList defined for selection/dropdown list manipulation. I think this is the cause of automapper not working, but I am totally clueless how to get by this.

Can anyone please help? The idea is simply to map common fields from model to/from viewmodel, while leaving out distinct fields. I hope I dont have to just manually map them writing tens of lines of code...

Lord Yggdrasill
  • 3,197
  • 4
  • 26
  • 42

1 Answers1

1

Just ignore that property.

Mapper.CreateMap<UserProfile, UserProfileViewModel>()
        .Ignore(dst => dst.AttitudeList);

Ignore mapping one property with Automapper

Community
  • 1
  • 1
VikciaR
  • 3,324
  • 20
  • 32
  • I see, it will work nice if the number of fields to ignore is like 1-2, but can still get big and messy if both model and viewmodel have 2-3+ unmatched fields. Is there a way to simply ignore any uncommon fields? Or is this not possible? – Lord Yggdrasill Mar 19 '15 at 07:20
  • Or this: http://stackoverflow.com/questions/4367591/automapper-how-to-ignore-all-destination-members-except-the-ones-that-are-mapp – VikciaR Mar 19 '15 at 07:22
  • 2
    The problem with "ignore the rest" is it silently breaks if you've mistyped something. You can always do ReverseMap, which preserves the Ignores. Or you can use the Ignore attribute on your viewmodels. – Jimmy Bogard Mar 19 '15 at 13:12
  • I am totally agree with Jimmy Bogard, also I tend to map ALL properties manually. It is some additional work, but I assure, that nothing will break if I rename property later. – VikciaR Mar 19 '15 at 14:29
  • @VikciaR: It seems that my AutoMapper works a bit differently, it uses the Map(userProfile) instead of CreateMap(UserProfile, UserProfileViewModel). The extension method does not seem to work if I just use Map rather than CreateMap. Why is this happening, and is there a way to get by this? – Lord Yggdrasill Mar 19 '15 at 17:02
  • @Lord: it works same. First do: Mapper.CreateMap (it will create map for mapping between your types, you can say: creates configuration). It is needed to do once (configuration internally is saved in static variable). And next, Mapper.Map. – VikciaR Mar 20 '15 at 06:18