9
votes

I have an IList<AdminVAT> and I'd like to copy this collection to IList<AdminVATDto> collection

I tried this :

IList<AdminVAT> listAdminVAT = new AdministrationService(session).ListDecimal<AdminVAT>();
AutoMapper.Mapper.CreateMap<IList<AdminVAT>, List<AdminVATDTO>>();
var res = AutoMapper.Mapper.Map<IList<AdminVAT>, IList<AdminVATDTO>>(listAdminVAT);

I receive this exception :

Trying to map System.Collections.Generic.IList`1[[AdminVAT, eSIT.GC.DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.IList`1[[AdminVATDTO, eSIT.GC.WebUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].

Thanks.

Update1

public class AdminVAT : IAdminDecimal
{
    public virtual int Id { get; set; }
    public virtual int Code { get; set; }
    public virtual decimal Value { get; set; }
}
public class AdminVATDTO : AdminVAT
{
    [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
    public override decimal Value { get; set; }
}

I still have 5 decimal in my dropdown list ...

Controller :

IList<AdminVAT> listAdminVAT = new AdministrationService(session).ListDecimal<AdminVAT>();
AutoMapper.Mapper.CreateMap<AdminVAT, AdminVATDTO>();
model.ListVAT = AutoMapper.Mapper.Map<IList<AdminVAT>, IList<AdminVATDTO>>(listAdminVAT);

HTML :

@Html.DropDownList("ddVAT", new SelectList(Model.ListVAT, "Id", "Value", Model.Estimation.AdminVAT))
1

1 Answers

22
votes

Define the mapping only between the simple types as explained in the documentation:

AutoMapper.Mapper.CreateMap<AdminVAT, AdminVATDTO>();

Then you will be able to convert between lists, collections, enumerables of those types:

IList<AdminVATDTO> res = AutoMapper.Mapper.Map<IList<AdminVAT>, IList<AdminVATDTO>>(listAdminVAT);