Omar AMEZOUG
Omar AMEZOUG

Reputation: 998

How to make mapping between List<string> and a string?

I have a Model an ViewModel Like this, but AutoMapper doesn't pass the values from MyViewModel to MyModel!

MyModel:

public List<string> ContentLinks { get; set; }
public string ListOfContentLinks {
    get
    {
        return String.Join(";", ContentLinks);
    }
    set {
        ContentLinks = value.Split(';').ToList();
    } 
}

MyViewModel:

public List<string> ContentLink { get; set; }

Boostrapper:

Mapper.CreateMap<MyViewModel, MyModel>();

What do I have to do to make the mapping work correctly?

Upvotes: 1

Views: 107

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236318

If you don't want to have properties with same name, then use custom mapping for that member:

Mapper.CreateMap<MyViewModel, MyModel>()
      .ForMember(d => d.ContentLinks, opt => opt.MapFrom(s => s.ContentLink));

Upvotes: 1

Dmitry Efimenko
Dmitry Efimenko

Reputation: 11188

properties must have the same name for the default mapping. You have ContentLinks in one case and ContentLink in another

Upvotes: 1

Related Questions