Сопоставить несколько разных моделей в одну View Model - AutoMapper - PullRequest
0 голосов
/ 27 марта 2019

Я пытаюсь отобразить несколько моделей Userprofile в одну модель представления. Одновременно будут отображаться только одни пользовательские данные.

В приведенном ниже фрагменте кода я упомянул пример использования, который уже пробовал. Кроме того, я пытаюсь AutoMapper.Map<source,target>, но у меня не получилось.

// Это одна модель FarmerUser

 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; }     
   }

Другая модель пользователя Tso

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; }

   }

Это моя ViewModel, в которой я пытаюсь объединить мои данные 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; }    
    }

Ниже приведен код, который я пытаюсь использовать.

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;
}

Ожидаемый результат должен состоять в том, что обе модели могут быть отображены в viewModel. Например, если я сопоставляю FarmerProfile, он должен быть сопоставлен, а если я хочу сопоставить TsoProfile, он тоже должен отображаться.

...