繁体   English   中英

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

[英]How to actually map Model object into a ViewModel object using AutoMapper in ASP.Net core 2?

在我的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文档后我找不到任何东西,这对我来说似乎真的很模糊,特别是因为我是新手

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

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

暂无
暂无

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

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