Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ValueResolver if field type does not have a default constructor?

I have model with property:

public class MyModel{
       public SelectList PropertyTypeList { get; set; }
}

And I have ValueResolver

public class MyPropertyValueResolver : ValueResolver<ProductProperty, SelectList>
{
    protected override SelectList ResolveCore(ProductProperty source)
    {
        myList = .......;
        return new SelectList(myList, "Value", "Text");
    }
}

Then I configure mapping

    Mapper.CreateMap<Source, Destination>()
          .ForMember(s => s.PropertyTypeList, opt => opt.ResolveUsing<MyPropertyValueResolver>());

But it says me that

Type 'System.Web.Mvc.SelectList' does not have a default constructor 

What I should to do to make it work?

like image 777
Roman Bats Avatar asked Oct 22 '22 21:10

Roman Bats


1 Answers

Rather than automapping to a SelectList, have you considered automapping to a simple Array, and then using a Get-only property to wrap this as a SelectList?

This answer describes the approach.

Also, from the same SO question, there is the ConstructedBy idea, as well as a way to use MapFrom to do this directly.

like image 141
Holf Avatar answered Nov 01 '22 16:11

Holf