0

I am trying to map multiple Userprofile models into a single viewModel. At a time only one user data will be mapped.

In the below code snippet I mentioned the use case that I already tried. Also, I try to AutoMapper.Map<source,target> but didn't work for me.

//This is one FarmerUser Model

 public partial class FarmerProfileModel { public long FarmerId { get; set; } public string FarmerName { get; set; } public string FatherHusbandName { get; set; } public string Cnic { get; set; } public string Gender { get; set; } public string CellPhone { get; set; } public string PresentAddress { get; set; } public string PermanentAddress { get; set; } public int? EducationCode { get; set; } public int? MaleDependant { get; set; } public int? FemaleDependant { get; set; } public string AlternateName { get; set; } public string AlternateCellPhoneNo { get; set; } public string AlternateRelationshipwithFarmer { get; set; } public int? ReferalCode { get; set; } public string FarmerImage { get; set; } public string UnionCouncil { get; set; } public string MozaName { get; set; } public int? SmallAnimals { get; set; } public int? BigAnimals { get; set; } public int? Tractor { get; set; } public Guid? UserGuid { get; set; } public DistrictCodeModel DistrictCodeNavigation { get; set; } public TehsilCodeModel TehsilCodeNavigation { get; set; } } 

Another Model of the Tso User

public class TsoProfileModel { public long Tsoid { get; set; } public string Tsoname { get; set; } public string Cnic { get; set; } public string Email { get; set; } public string CellPhone { get; set; } public string AlternateCellPhone { get; set; } public string Landline { get; set; } public string Gender { get; set; } public string Tsoimage { get; set; } public int? ModifiedBy { get; set; } public DateTime? ModifiedDateTime { get; set; } public DateTime? InsertionDate { get; set; } public string ActiveStatus { get; set; } public Guid? UserGuid { get; set; } public TbDistrictCode DistrictCodeNavigation { get; set; } public TbTehsilCode TehsilCodeNavigation { get; set; } } 

This is my ViewModel in which i am trying to incorporrate my data of Farmer/Tso

 public class UserProfileInfo { public long? UserId { get; set; } public string UserName { get; set; } public string Cnic { get; set; } public string Email { get; set; } public string AlternateCellPhone { get; set; } public string Landline { get; set; } public string Gender { get; set; } public string PresentAddress { get; set; } public string PermanentAddress { get; set; } public int? EducationCode { get; set; } public string image { get; set; } public int? ModifiedBy { get; set; } public DateTime? ModifiedDateTime { get; set; } public DateTime? InsertionDate { get; set; } public string ActiveStatus { get; set; } public Guid? UserGuid { get; set; } public string FatherHusbandName { get; set; } public string CellPhone { get; set; } public int? MaleDependant { get; set; } public int? FemaleDependant { get; set; } public string AlternateName { get; set; } public string AlternateRelationshipwithFarmer { get; set; } public int? ReferalCode { get; set; } public string UnionCouncil { get; set; } public string MozaName { get; set; } public int? SmallAnimals { get; set; } public int? BigAnimals { get; set; } public int? Tractor { get; set; } public string Role { get; set; } public DistrictCodeModel DistrictCodeNavigation { get; set; } public TehsilCodeModel TehsilCodeNavigation { get; set; } } 

Below is the code I am trying to use.

if (User=="farmer") { var tbFarmerInfo = await _farmerService.GetFarmerByCellPhone(cellno); var result = _Mapper.Map<UserProfileInfo>(tbFarmerInfo); result.UserId = tbFarmerInfo.FarmerId; result.UserName = tbFarmerInfo.FarmerName; result.image = tbFarmerInfo.FarmerImage; result.Role = "Farmer"; response.Data = result; return response; } else if (User == "TSO") { string cellno = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value.TrimStart('0'); var tbTsoInfo = await _tsoService.GetTSOByCellPhone(cellno); var result = _Mapper.Map<UserProfileInfo>(tbTsoInfo); result.UserId = tbTsoInfo.Tsoid; result.UserName = tbTsoInfo.Tsoname; result.image = tbTsoInfo.Tsoimage; result.Role = "TSO"; response.Data = result; return response; } 

The expected result should be that both models can be mapped in the viewModel. Like, if I map the FarmerProfile it should be mapped and if I want to map TsoProfile it should be mapped too.

4
  • 1
    Possible duplicate of Automapper - Multi object source and one destination Commented Mar 27, 2019 at 16:18
  • @JSteward No, it's not. Please have a look at the models. Both models have different fields but I want to map them to one View Model. Commented Mar 27, 2019 at 18:25
  • Can you not handle that with your mapping configurations? Mapping different properties shouldn't be an issue? Commented Mar 27, 2019 at 18:27
  • I want it to be mapped in the same view model. So that I have to create only one model for future use rather than creating different models for the user profiles. Commented Mar 27, 2019 at 18:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.