asp.net-mvc – 如何为具有IEnumerable属性的ViewModel使用automapper

我试图遵循
Jimmy Bogard’s advice for automapping through an actionfilter(在大多数情况下效果很好).但是,如果我有一个自定义的viewmodel,我想要映射的集合属性呢?例如,

public class WidgetSearchViewModel
{
    public WidgetSearchOptionsViewModel Options { get; set; }
    public GenericListPagerViewModel Pager { get; set; }
    public IEnumerable<WidgetSearchResultModel> Results { get; set; }
}

Results属性来自作为IEnumerable域实体的存储库,我想使用automapper将其转换为WidgetSearchResultModel实例.问题是我需要在控制器中构建一个WidgetSearchViewModel来填充Options和Pager属性.如何使用AutoMapper ActionFilter填充Results属性OnActionExecuted?它是否可能,或者我是否需要在控制器中调用Mapper.Map调用并在我的单元测试中引导所有映射?

最佳答案 Automapper已经构建了对实现IEnumerable的任何嵌套集之间的映射的支持.如果定义父和子属性映射,则automapper将映射嵌套集合.

如果您实现CustomTypeConvertor:http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&referringTitle=Home,Automapper还可以处理将任何内容映射到任何内容

点赞