简体   繁体   English

MVC-使用AutoMapper发送集合视图模型

[英]MVC - Sending collection View Model using AutoMapper

I have a fairly simple example of a mapping that is working, but I'm not convinced that I'm doing the correct thing. 我有一个非常简单的映射示例,但是我不确信自己在做正确的事情。 Could somebody help me understand what the best practise is with sending collections through to the View Model? 有人可以帮助我了解将收藏集发送到视图模型的最佳实践吗?

This is the AutoMapper.CreateMap: 这是AutoMapper.CreateMap:

Mapper.CreateMap<Product, _BrowseViewModel>();

This is the Controller: 这是控制器:

var dsProduct = from p in Entity.Product
select p;

if (dsProduct.Any())
{
   IEnumerable<_BrowseViewModel> browseViewModel = Mapper.Map<IEnumerable<Product>, IEnumerable<_BrowseViewModel>>(dsProduct.ToList());
   return View(browseViewModel);
}

The _BrowseViewModel class only contains a few fields from the Product table that will be displayed: _BrowseViewModel类仅包含Product表中将显示的几个字段:

public class _BrowseViewModel
{

    public long ProductID { get; set; }
    public string ProductName { get; set; }
    ... etc

My concern is that the Model in the view is now a collection; 我担心的是,视图中的模型现在是一个集合。 which does not seem right. 这似乎不正确。

@model IEnumerable<AppStoreModel._BrowseViewModel>

What is the correct architecture for this particular scenario? 此特定方案的正确架构是什么?

The best way to do what you're trying to achieve do is by using AutoMapper's projection feature, like so: 要做您要实现的目标的最佳方法是使用AutoMapper的投影功能,如下所示:

IEnumerable<BrowseProduct> browseViewModel = dsProduct.Project().To<BrowseProduct>().ToList();

using the following AutoMapper configuration: 使用以下AutoMapper配置:

Mapper.Map<Product, BrowseProduct>();

If you're sending data to the view and are concerned about sending the data as a list, then I would recommend sending a view model to the view that contains your list of products, eg: 如果您要向视图发送数据并且担心将数据作为列表发送,那么我建议向包含您的产品列表的视图发送视图模型,例如:

public class BrowseProductsViewModel
{
    public string ProductCategory { get; set;}

    public IEnumerable<BrowseProduct> Products { get; set; }
}

This introduction of the view model will allow you to pass new data to your view without the need to update the view model type within your razor views. 视图模型的这种引入将使您可以将新数据传递到视图,而无需在剃刀视图中更新视图模型类型。

It seems that having Product as a child class is working better (Thanks MajoB). 似乎将Product作为儿童班工作得更好(谢谢MajoB)。

Here is the code for anyone battling with mapping collections. 这是任何与映射集合作斗争的人的代码。

ViewModel: ViewModel:

public class _BrowseViewModel
{
    public IEnumerable<_BrowseProduct> BrowseProduct { get; set; }
}

public class _BrowseProduct
{
    public long ProductID { get; set; }
    public string ProductName { get; set; }
    ... etc
}

AutoMapper Config: AutoMapper配置:

Mapper.CreateMap<Product, _BrowseProduct>();

Controller: 控制器:

 _BrowseViewModel browseViewModel = new _BrowseViewModel();
 browseViewModel.BrowseProduct = Mapper.Map<IEnumerable<Product>, IEnumerable<_BrowseProduct>>(dsProduct);

return View(browseViewModel);

I think this is quite a good way of achieving what I set out to do. 我认为这是实现我打算要做的事情的一种很好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM