0

我试图映射这些,但我得到了

AutoMapper.AutoMapperMappingException:“缺少类型映射配置或不支持的映射。”

public class MenuItemViewModel
{
    [Required]
    public string Text { get; set; }
    public string URL { get; set; }
    public string Icon { get; set; }
    public IList<MenuItemViewModel> Children { get; set; }
    public MenuItemViewModel Parent { get; set; }
    public int Sort { get; set; }
}

public class MenuItem
{
    public Guid Id { get; set; }
    public string Text { get; set; }
    public string URL { get; set; }
    public string Icon { get; set; }
    public IList<MenuItem> Children { get; set; }
    public MenuItem Parent { get; set; }
    public bool onMenu { get; set; }
    public int Sort { get; set; }

    public IList<MenuItemPermittedRole> MenuItemPermittedRoles { get; set; }
}

Mapper.Initialize(cfg => {
    cfg.CreateMap<MenuItem, MenuItemViewModel>().ReverseMap();
});

MenuItemViewModel result = Mapper.Map<MenuItemViewModel>(menuItems); ERROR HERE
4

1 回答 1

4

如果不确切知道您收到哪个错误,很难回答,但我可以猜到......如果 menuItems 是一个IEnumerable<MenuItem>,你应该写

var result = Mapper.Map<IEnumerable<MenuItemViewModel>>(menuItems);
于 2017-10-27T22:39:05.860 回答