Roman Bats
Roman Bats

Reputation: 1795

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?

Upvotes: 5

Views: 748

Answers (1)

Holf
Holf

Reputation: 6431

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.

Upvotes: 3

Related Questions