Saturday, March 10, 2018

Part 52 - How to use Automapper in Asp.net MVC




In this video you will be able to know about how to use Automapper in  Asp.net MVC. 

#AutoMapper Initialization (AutomapperWebProfile.cs)
 Create a folder Infrastructure in the root folder of the project and create a class AutomapperWebProfile and copy below code into this.

using MVCTutorial.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCTutorial.Infrastructure
{
    public class AutomapperWebProfile : AutoMapper.Profile
    {
        public AutomapperWebProfile()
        {

            CreateMap<EmployeeDomainModel, EmployeeViewModel>();

            CreateMap<EmployeeViewModel, EmployeeDomainModel>();

        }

        public static void Run()
        {
            AutoMapper.Mapper.Initialize(a =>
            {
                a.AddProfile<AutomapperWebProfile>();


            });
        }

       

    }
}

#EmployeeDomainModel.cs 
Create below class into Model folder

 public class EmployeeDomainModel
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }

    }

# EmployeeViewModel.cs 

Create below class into Model folder

public class EmployeeViewModel
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
       
    }


# Controller Code (TestController.cs)
Create a Test controller and in your Index method write below code. Here, I have added few record in EmployeeDomainModel and using AutoMapper, I copied data into EmployeeViewModel directly. In this way you can use Automapper. 

        public ActionResult Index()
        {
            List<EmployeeDomainModel> empDomainList = new List<EmployeeDomainModel>();

            empDomainList.Add(new EmployeeDomainModel { EmployeeId = 1, Name = "Ashish" });
            empDomainList.Add(new EmployeeDomainModel { EmployeeId = 2, Name = "Ajay" });
            
            List<EmployeeViewModel> empVMList = new List<EmployeeViewModel>();            
            
            AutoMapper.Mapper.Map(empDomainList, empVMList);

            return View();
        }



#Global.asax file
Add below code into Application_Start() Method. to Initialize Automapper and to create map between source and destination folder. 

  AutomapperWebProfile.Run();

Thanks  . Keep Learning and Sharing


All Code Factory


4 comments:

George Morgan said...

This is really informative article for new learner. Author explained all the things very nicely in this article and everyone can easily understand it. Really appreciated

Now buffalo website design service is available from Crystal Tech Solution in a professional way.

Sharon Wood said...

Avast Support
McAfee Phone Number
Norton Support Number

Dale Morris said...

Norton Antivirus Support phone Number
Mcafee Customer Service
Malwarebytes tech support phone number
hp printer support chat
canon printer tech support telephone number

Daniel Smith said...


I did not know the entire benefits of Garmin Nuvi Update. But since I have read this blog on map update, I have installed all available updates for my garmin device. I must say that this piece of blog has helped me improve navigation experience. I am not suggesting all my friends to read this blog and get Garmin.com/Express with this team. For detailed information, you can contact sus at toll-free number +1 888-309-0939. Our Garmin GPS professionals will provide instant help.