我有一个带有属性的模型:
public class MyModel{
public SelectList PropertyTypeList { get; set; }
}
我有 ValueResolver
public class MyPropertyValueResolver : ValueResolver<ProductProperty, SelectList>
{
protected override SelectList ResolveCore(ProductProperty source)
{
myList = .......;
return new SelectList(myList, "Value", "Text");
}
}
然后我配置映射
Mapper.CreateMap<Source, Destination>()
.ForMember(s => s.PropertyTypeList, opt => opt.ResolveUsing<MyPropertyValueResolver>());
但它告诉我
Type 'System.Web.Mvc.SelectList' does not have a default constructor
我应该怎么做才能让它发挥作用?
最佳答案
您是否考虑过自动映射到一个简单数组,然后使用 Get-only 属性将其包装为一个 SelectList,而不是自动映射到 SelectList?
This answer描述了该方法。
此外,在同一个 SO 问题中,还有 ConstructedBy想法,以及使用 MapFrom
直接执行此操作的方法。
关于c# - 如果字段类型没有默认构造函数,如何使用 ValueResolver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14084594/