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))
AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.
So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.
Where do I configure AutoMapper? ¶ Configuration should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With