1

在我的 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 文档后我什么也想不通,这对我来说似乎真的很模糊,尤其是因为我是新手

4

1 回答 1

1

映射枚举是内置的。只要有泛型类型的映射,AutoMapper 就可以映射到这些类型的枚举。换句话说:

var displayDonatorViewModels = _mapper.Map<IEnumerable<DisplayDonatorViewModel>>(dataRecords);
于 2019-05-08T16:05:57.057 回答