如何使用ASP.Net core 2中的AutoMapper将Model对象实际映射到ViewModel对象?

时间:2019-05-08 15:51:12

标签: c# asp.net-core automapper

在我的ASP.NET类上,有人告诉我开发一个简单的应用程序,该程序从.csv文件读取数据,然后在视图中显示它们。我有一个要从.csv文件导入数据的模型。我还有一个ViewModel,其中包含我实际上要在视图中显示的属性。如何使用AutoMapper处理将模型映射到ViewModel对象?

我为要执行的映射创建了一个配置文件,我在Startup.cs文件中注册了配置。每当我想在控制器中实际进行映射时,我都会碰壁,因为我不知道如何处理Enumerables映射

我的模型课:

public class Donation
    {
        public string First_Name { get; set; }
        public string Last_Name { get; set; }
        public long Pesel { get; set; }
        public string Donation_date { get; set; }
        public string Donated_blood_amount { get; set; }
        public string Blood_type { get; set; }
        public string Blood_factor { get; set; }
        public string Address { get; set; }
    }

我的ViewModel类:

public class DisplayDonatorViewModel
    {
        public string First_Name { get; set; }
        public string Last_Name { get; set; }
        public string Donated_blood_amount { get; set; }

    }

我的AutoMapper配置文件类:

public class DisplayDonatorViewModelProfile : Profile
    {
        public DisplayDonatorViewModelProfile()
        {

            CreateMap<Donation, DisplayDonatorViewModel>()
                .ForMember(destination => destination.First_Name, h => h.MapFrom(source => source.First_Name))
                .ForMember(destination => destination.Last_Name, h => h.MapFrom(source => source.Last_Name))
                .ForMember(destination => destination.Donated_blood_amount, h => h.MapFrom(source => source.Donated_blood_amount));



        }
    }

Startup.cs中的配置

var config = new MapperConfiguration(cfg => {
                cfg.AddProfile<DisplayDonatorViewModelProfile>();
            });

            var mapper = config.CreateMapper();

现在要解决的主要问题是控制器

public class DonationsController : Controller
    {
        private readonly IHostingEnvironment _env;
        private readonly IMapper _mapper;
        public DonationsController(IHostingEnvironment env, IMapper mapper)
        {
            _env = env;
            _mapper = mapper;
        }

        public IActionResult Index()
        {
            string webRootPath = _env.WebRootPath;
            string dataFolder = "data";
            string fileName = "MOCK_DATA.csv";
            string csvFilePath = Path.Combine(webRootPath, dataFolder, fileName);
            IEnumerable<Donation> dataRecords;
            IEnumerable<DisplayDonatorViewModel> displayDonatorViewModels;

            using (var reader = new StreamReader(csvFilePath))
            using (var csv = new CsvReader(reader))
            {
                dataRecords = csv.GetRecords<Donation>().ToList();

            }            
            displayDonatorViewModels = _mapper.Map<Donation, DisplayDonatorViewModel>(dataRecords); //does not work, "cannot convert from 'System.Collections.Generic.IEnumerable<BloodDonatorsApp.Models.Donation>' to 'BloodDonatorsApp.Models.Donation'

            return View(dataRecords);
        }
    }

dataRecords是一个IEnumerable变量,包含来自csv的数据。我想将此对象及其数据映射到IEnumerable displayDonatorViewModels并将其传递到我的视图,而不是传递枚举的Donation对象。

解决方案可能真的很简单,我错过了一些简单的东西,但是在查看AutoMapper文档后我找不到任何东西,这对我来说确实很模糊,尤其是因为我是新手

1 个答案:

答案 0 :(得分:1)

映射可枚举是内置的。只要存在通用类型的映射,AutoMapper便可以映射到这些类型的枚举。换句话说:

var displayDonatorViewModels = _mapper.Map<IEnumerable<DisplayDonatorViewModel>>(dataRecords);