Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Property of one type map to another type of instance

I am using automapper for mapping view models and entity models with each other, all was good, but now i have a little different scenario where AutoMapper is not able to map my types.

My View Model:

   public class CriminalSearchViewModel
   {

    public CriminalSearchParamsViewModel SearchParameters { get; set; }

    public SelectList GenderSelectList { get; set; }

    public SelectList NationalitySelectList { get; set; }

    public SelectList CrimeSelectList { get; set; }

    public SelectList CriminalStatusSelectList { get; set; }
}

second view model:

public class CriminalSearchParamsViewModel
{
    [Required]
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int? GenderID { get; set; }

    public int? StatusID { get; set; }

    public string CNIC { get; set; }

    public int? AgeFrom { get; set; }

    public int? AgeTo { get; set; }

    public double? Height { get; set; }

    public int Weight { get; set; }

    public int? NationalityID { get; set; }
}

and my Business Model:

public class CriminalSearch
{

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int? GenderID { get; set; }

    public int? StatusID { get; set; }

    public string CNIC { get; set; }

    public int? AgeFrom { get; set; }

    public int? AgeTo { get; set; }

    public double? Height { get; set; }

    public int Weight { get; set; }

    public int? NationalityID { get; set; }

}

I have defined mapping like:

Mapper.CreateMap<CriminalSearch, CriminalSearchParamsViewModel>();

also tried this as well:

 Mapper.CreateMap<CriminalSearchParamsViewModel,CriminalSearchViewModel>()
  .ForMember(dest => dest.SearchParameters, opt =>
     opt.MapFrom(src => Mapper.Map<CriminalSearchParamsViewModel, CriminalSearch>(src)));

and in controller i am trying like:

public ActionResult Search(CriminalSearchViewModel searchVM)
{
    if (ModelState.IsValid)
    {
        var searchParams = searchVM.SearchParameters;
        var criminalSearch = AutoMapper.Mapper.Map<CriminalSearch>(searchParams);
        _criminalService.SearchCriminals(criminalSearch);
    }
        return View();
}

But it always throws exception:

Missing type map configuration or unsupported mapping.

Mapping types: CriminalSearchParamsViewModel -> CriminalSearch NationalCriminals.UI.ViewModels.CriminalSearchParamsViewModel -> NationalCriminals.Core.Models.CriminalSearch

Destination path: CriminalSearch

Source value: NationalCriminals.UI.ViewModels.CriminalSearchParamsViewModel

Anybody can pint me what is going wrong?

Upvotes: 2

Views: 80

Answers (1)

Joel R Michaliszen
Joel R Michaliszen

Reputation: 4222

You just need to change the order of the generic args in the method CreateMap:

Mapper.CreateMap<CriminalSearchParamsViewModel,CriminalSearch>()

Thats because the first generic arg is the Source type and the second is the Destination, it is not two way, you must to declare the both if you want to map from a type to another and viceversa like this:

Mapper.CreateMap<CriminalSearchParamsViewModel,CriminalSearch>()
Mapper.CreateMap<CriminalSearch,CriminalSearchParamsViewModel>()

The method CreateMap is described like this:

AutoMapper.Mapper.CreateMap<SourceClass, DestinationClass>();

Suggest: Using AutoMapper: Creating Mappings

Upvotes: 2

Related Questions