AutoMapper无法从实体模型转换为viewmodel

时间:2018-01-12 12:02:03

标签: c# entity-framework asp.net-core-mvc automapper

我只是按照this guide安装并设置了AutoMapper,我想我已经正确设置了它。但是当我尝试从IEnumerable<ProductCategory>映射到IEnumerable<ViewModelProductCategory>时,就像这样:

var categories = _context.ProductCategories.Include(e => e.Children).ToList();
var topLevelCategories = categories.Where(e => e.ParentId == null);
var VMCategory = _mapper
    .Map<IEnumerable<ProductCategory>, 
        IEnumerable<ViewModelProductCategory>>(topLevelCategories);
return View(VMCategory);

(感谢@Anton Gorbunov,指出我交换了对象!)

...我的观点抱怨收到错误的模特。我很确定这是因为子类别没有映射到子列表,因此在视图中,item.Children未正确设置为视图模型。

如何映射列表列表?

这是我的索引视图:

@model IEnumerable<MyStore.Models.ViewModels.ViewModelProductCategory>
<ul>
    @Html.Partial("_CategoryRecursive", Model)
</ul>

...这是_CategoryRecursive.cshtml:

@model IEnumerable<MyStore.Models.ViewModels.ViewModelProductCategory>
<ul style="list-style:none;padding-left:0px;">
   @if (Model != null)
    {
        foreach (var item in Model)
        {
            <li style="margin:8px 0px 0px 0px;">
                <ul>
                    @Html.Partial("_CategoryRecursive.cshtml", item.Children)
                </ul>
            </li>
        }
    }
</ul>

映射配置文件:

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<ProductCategory, ViewModelProductCategory>();
        CreateMap<ViewModelProductCategory, ProductCategory>();
        CreateMap<Product, ViewModelProduct>();
        CreateMap<ViewModelProduct, Product>();
    }
}

实体模型:

public class ProductCategory
{
    public int Id { get; set; }
    public int SortOrder { get; set; }
    public string Title { get; set; }

    [ForeignKey(nameof(ParentCategory))]
    public int? ParentId { get; set; }

    public ProductCategory ParentCategory { get; set; } //nav.prop to parent
    public ICollection<ProductCategory> Children { get; set; } //nav. prop to children

    public List<ProductInCategory> ProductInCategory { get; set; }
}

视图模型:

public class ViewModelProductCategory
{
    public int Id { get; set; }
    public int? ParentId { get; set; }
    public string Title { get; set; }
    public int SortOrder { get; set; }

    public string ProductCountInfo
    {
        get
        {
            return Products != null && Products.Any() ? Products.Count().ToString() : "0";
        }
    }

    public ProductCategory ParentCategory { get; set; } // Nav.prop. to parent
    public IEnumerable<ProductCategory> Children { get; set; } // Nav.prop. to children

    public List<ViewModelProduct> Products { get; set; } // Products in this category
    public List<ViewModelProduct> OrphanProducts { get; set; } // Products with no references in ProductInCategory
}

1 个答案:

答案 0 :(得分:1)

问题在这里:

var VMCategory = _mapper.Map<IEnumerable<ViewModelProductCategory>, IEnumerable<ProductCategory>>(topLevelCategories);

您应该将代码更改为

var VMCategory = _mapper.Map<IEnumerable<ProductCategory>, IEnumerable<ViewModelProductCategory>>(topLevelCategories);

描述: TDestination = Map<TSource,TDestination>(TSource sourceObject),但您在括号内翻了类型TSourceTDestination&lt;,&gt;