代码之家  ›  专栏  ›  技术社区  ›  Stian

为什么无法将此模型映射到此viewmodel?

  •  1
  • Stian  · 技术社区  · 7 年前

    我有一个递归结构类别树的实体模型:

    public class ProductCategory
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Title { get; set; }
        public int SortOrder { get; set; }
    
        public ProductCategory ParentCategory { get; set; } //nav.prop to parent
        public ICollection<ProductCategory> Children { get; set; } = new List<ProductCategory>();
        public ICollection<ProductInCategory> ProductInCategory { get; set; }
        public ICollection<FrontPageProduct> FrontPageProduct { get; set; } // Nav.prop. to front page product
    
        // Recursive sorting:
        public void RecursiveOrder()
        {
            Children = Children.OrderBy(o => o.SortOrder).ToList();
            Children.ToList().ForEach(r => r.RecursiveOrder());
        }
    }
    

    ... 这应该是匹配的ViewModel:

    public class ViewModelProductCategory
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Title { get; set; }
        public int SortOrder { get; set; }
        public bool Checked { get; set; } // Used for assigning a product to multiple categories in Product/Edit
    
        // Nav.props:
        public ViewModelProductCategory ParentCategory { get; set; } // Nav.prop. to parent
        public ICollection<ViewModelProductCategory> Children { get; set; } // Nav.prop. to children
        public IEnumerable<ViewModelProduct> Products { get; set; } // Products in this category
        public IEnumerable<ViewModelFrontPageProduct> FrontPageProducts { get; set; }
    
        public string ProductCountInfo { get { return Products?.Count().ToString() ?? "0"; } }
    }
    

    当我尝试填充viewmodel时,如下所示:

    List<ProductCategory> DbM = 
        await _context.ProductCategories
            .Include(c => c.Children)
            .Where(x => x.ParentId == null)
            .OrderBy(o => o.SortOrder)
            .ToListAsync();
    foreach (var item in DbM)
    {
        VMSelectCategories.Add(
            new ViewModelProductCategory{
                Id = item.Id,
                Children = item.Children,
                Title = item.Title
            });
    }
    

    VisualStudio对我大喊大叫,它无法隐式转换 ProductCategory ViewModelCategory . 这发生在 Children = item.Children .

    为什么不起作用?我不能在viewmodel中使用与原始实体模型无关的其他属性吗?喜欢 Checked ProductCountInfo ?

    1 回复  |  直到 7 年前
        1
  •  1
  •   jmcilhinney    7 年前

    在此行中:

    Children = item.Children,
    

    Children ViewModelProductCategory.Children 属性,类型为 ICollection<ViewModelProductCategory> 虽然 item.Children ProductCategory.Children 属性,类型为 ICollection<ProductCategory> . 它们是不同的类型,既不继承也不实现另一种类型,那么为什么您希望能够将一种类型的对象分配给另一种类型的属性呢?您是否希望这样做有效:

    var list1 = new List<int> {1, 2, 3};
    List<string> list2 = list1;
    

    当然不是(我希望),因为分配 List<int> 对象到 List<string> 变量将是愚蠢的。你想做的完全一样。您需要提供某种方法将一种类型转换为另一种类型,然后在代码中实现这种转换。可以这样选择:

    Children = item.Children.Select(pc => MapToViewModel(pc)).ToList(),
    

    哪里 MapToViewModel 是您编写的用于创建 ViewModelProductCategory 并从 ProductCategory 参数

    您还可以考虑使用AutoMapper之类的工具。