[英]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 我有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? 我应该做些什么才能让它发挥作用?
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? 您是否考虑自动化到一个简单的数组,然后使用Get-only属性将其包装为SelectList,而不是自动转移到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. 此外,从相同的SO问题,有ConstructedBy的想法,以及使用
MapFrom
直接执行此操作的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.