【发布时间】:2021-01-20 05:11:41
【问题描述】:
我做了一些研究,但找不到我想要的。
我有无穷无尽的菜单。我有一个用于此菜单的 MenuDTO 和一个 MenuViewModel。我在模型和 DTO 之间匹配没有问题,但是在将 DTO 映射到 ViewModel 时遇到了问题。显然我找不到解决方案,你能帮忙吗?
我的 MenuDTO 对象
public class MenuDto : BaseDto
{
public string Name { get; set; }
public string Icon { get; set; }
public string Order { get; set; }
public string Url { get; set; }
public bool IsVisible { get; set; }
public int ParentId { get; set; }
public MenuDto ParentMenu { get; set; }
public List<MenuDto> Menus { get; set; }
}
还有 MenuViewModel
public class MenuViewModel
{
public int Id { get; set; }
public bool IsActive { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public string Order { get; set; }
public string Url { get; set; }
public bool IsVisible { get; set; }
public int ParentId { get; set; }
public MenuViewModel ParentMenu { get; set; }
public List<MenuViewModel> Menus { get; set; }
}
这就是我映射 MenuDTO 和 MenuViewModel 对象的方式。
public class WebProfile : Profile
{
public WebProfile()
{
CreateMap<MenuDto, MenuViewModel>();
CreateMap<MenuViewModel, MenuDto>();
}
}
我在控制器中这样调用
var navMenuItems = _mapper.Map<List<MenuViewModel>(_menuService.GetNavMenus());
虽然所有字段都已映射,但我在“菜单”字段上收到错误。
我得到的错误信息是;
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
MenuDto -> MenuViewModel
BiPortal2020.Business.ServiceDTOs.Menu.MenuDto -> BiPortal2020.WebUI.Areas.Admin.Models.Menu.MenuViewModel
lambda_method(Closure , MenuDto , MenuViewModel , ResolutionContext )
AutoMapperMappingException: Error mapping types.
Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1
【问题讨论】:
-
BaseDto中有什么内容? -
BaseDto 具有字段 Id、DateCreated、DateModified、IsActive。
-
我认为映射没有任何问题。请在此处更正错字 -
_mapper.Map<List<MenuViewModel>(_menuService.GetNavMenus())并让我们知道最新的错误消息。 -
抱歉打错字了,即使该行有 -
_mapper.Map<List<MenuViewModel>>(_menuService.GetNavMenus());,我仍然收到同样的错误 -
你检查过
_menuService.GetNavMenus()吗?正在返回List<MenuDto>?
标签: asp.net-core-mvc mapping automapper asp.net-core-3.0