AutoMapper - How to pass parameters into a Custom Resolver using ConstructedBy method?

十年热恋 提交于 2019-12-03 14:49:35

I found your posting trying to do the same thing. I decided on a simple approach and skip trying to map to my select list directly via AutoMaper. I simply return an array into my ViewModel and reference that object for my select list. The array gets mapped, select list object does not. Simple, effective. And, IMHO each is doing it's intended task - the mapper maps, the ViewModel does the layout

View Model code:
        [DisplayName("Criterion Type")]
        public virtual CriterionType[] CriterionTypes { get; set; }

        [DisplayName("Criterion Type")]
        public SelectList CriterionTypeList
        {
            get
            {
                return new SelectList(CriterionTypes, "Id", "Key");
            }
        }  

my mapper:

 Mapper.CreateMap<Criterion, CriterionForm>()
            .ForMember(dest => dest.CriterionTypeList, opt => opt.Ignore());     

I like that idea as a feature request. You can do something like that right now, with MapFrom:

ForMember(dest => dest.slUsers, opt => opt.MapFrom(src => new UsersSelectListResolver(src).Resolve(src));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!