简体   繁体   中英

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

On my ASP.NET classes I've been told to develop a simple app reading data from .csv file and then displaying them in a view. I have a Model to which I am importing the data from .csv file. I also have a ViewModel which contains properties I actually want to display in my view. How to handle mapping Model to a ViewModel object with AutoMapper?

I created a configuration profile for the mapping I'd like to perform, I registered configuration in Startup.cs file. I hit a wall whenever I want to actually do the mapping in my Controller because I dont know how to handle mapping Enumerables

My Model class:

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; }
    }

My ViewModel class:

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

    }

My AutoMapper profile class:

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));



        }
    }

Configuration inside Startup.cs

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

            var mapper = config.CreateMapper();

Now to main problem, here's the controller

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 is a IEnumerable variable with data from csv. I'd like to map this object and its data to a IEnumerable displayDonatorViewModels and pass it to my view instead of passing enumerable of Donation objects.

The solution is probably really easy and I am missing something simple but I couldnt figure out anything after looking in the AutoMapper documentation, it seems really vague to me especially since I am a newbie

Mapping enumerables is built-in. As long as there's mappings for the generic types, AutoMapper can map to enumerables of those types. In other words:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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