Thursday 19 May 2016

How to map your Web Access Layer List type of model to Data Access model in ASP.NET MVC by using Automapper.

In this article, I am going to show you how to map your Web Access Layer list type of model to Data Access Layer model. For that you have to use Automapper class for mapping that model classes.


Step 1:- Model class

namespace WebAccessLayer.Models
{
    public class DropListModel
    {
    public int? Id { get; set; }
        public string Title { get; set; }
    }
}

namespace DataAccessLayer.Models
{
    public class DropListModel
    {
    public int? Id { get; set; }
        public string Title { get; set; }
    }
}



Step 2:- Controller Code:-

[HttpPost]
public ActionResult Test(DropListModel oModel)
{
    AutoMapper.Mapper.Map<WebAccessLayer.Models.DropListModel , DataAccessLayer.Models.DropListModel>(oModel);
    return View(oModel);
}  


Mapping List type of Model code:-

public ActionResult Test2(List<DropListModel> oModel)
{
    AutoMapper.Mapper.Map<List<WebAccessLayer.Models.DropListModel>, List<DataAccessLayer.Models.DropListModel>>(oModel);
    return View(oModel);
}

No comments:

Post a Comment