Map View model and data model with auto mapper

581 Views Asked by At

I want to use auto mapper to map my Data Model to my View Model.

This is my class:

PersonViewModel.cs (same as my data Model):

public class PersonViewModel {
    public PersonViewModel(Person personDataModel) //I want to create map here of my PersonListDisplayModel with Person.cs(data model).
    {
        PersonListDisplayModel = new List<PersonListDisplayModel>();
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public List<PersonListDisplayModel> PersonListDisplayModel { get; set; }
}

public class PersonListDisplayModel {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

My View:

@model PersonViewModel
@using(Html.BeginForm("","",FormMethod.Post)){
//here i will add records
}
@Html.Partial("_personList", Model.PersonListDisplayModel) //Here i will load my partial view to display list of person but how do i pass here like this:Model.PersonListDisplayModel

This is my Controller:

public ActionResult Save() {
    PersonViewModel model = new PersonViewModel();
    using (var db = new MyDBContext()) {
        var data = db.Person.tolist();
        return View();
    }    
}

Note: I want to create map in my View model only and not in controller.

0

There are 0 best solutions below