asp.net-mvcasp.net-mvc-3automapperautomapping

Getting null values when i map using autoMapper


These are my Attribute Models

    public class MasterPricerTravelBoardSearchReplyModel
{
    public MasterPricerTravelBoardSearchReplyModel()
    {
        FlightIndex = new List<MasterPricerTravelBoardSearchReplyFlightIndexModel>();

    }
    public List<MasterPricerTravelBoardSearchReplyFlightIndexModel> FlightIndex
    { get; set; }

}

public class MasterPricerTravelBoardSearchReplyFlightIndexModel
{
    public MasterPricerTravelBoardSearchReplyFlightIndexModel()
    {
        RequestedSegmentRef = new OriginAndDestinationRequestType6Model();
        GroupOfFlights = new List<MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsModel>();
    }
    public OriginAndDestinationRequestType6Model RequestedSegmentRef { get; set; }
    public List<MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsModel> GroupOfFlights { get; set; }
}

public class OriginAndDestinationRequestType6Model
{
    public string SegRef { get; set; }
}

 public class MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsModel
{
    public MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsModel()
    {
        FlightDetails = new List<MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsFlightDetailsModel>();
    }
    public List<MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsFlightDetailsModel> FlightDetails { get; set; }
}

public class MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsFlightDetailsModel
{
    public MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsFlightDetailsModel()
    {
        FlightInformation = new TravelProductType2Model();
    }
    public TravelProductType2Model FlightInformation { get; set; }
}

public class TravelProductType2Model
{
    public TravelProductType2Model()
    {
        ProductDateTime = new ProductDateTimeType8Model();
        Location = new List<LocationIdentificationDetailsType2Model>();
        CompanyId = new CompanyIdentificationType8Model();
        ProductDetail = new AdditionalProductDetailsType3Model();
        AddProductDetail = new ProductFacilitiesType2Model();
    }
    public ProductDateTimeType8Model ProductDateTime { get; set; }
    public List<LocationIdentificationDetailsType2Model> Location { get; set; }
    public CompanyIdentificationType8Model CompanyId { get; set; }
    public string FlightOrtrainNumber { get; set; }
    public AdditionalProductDetailsType3Model ProductDetail { get; set; }
    public ProductFacilitiesType2Model AddProductDetail { get; set; }
    public decimal TotalAmount { get; set; }
    public string FlightName { get; set; }
    public string CabinName { get; set; }
}

public class LocationIdentificationDetailsType2Model
{
    public string LocationId { get; set; }
    public string AirportCityQualifier { get; set; }
    public string Terminal { get; set; }
}

public class CompanyIdentificationType8Model
{
    public string MarketingCarrier { get; set; }
    public string OperatingCarrier { get; set; }
}

public class AdditionalProductDetailsType3Model
{
    public string EquipmentType { get; set; }
    public string OperatingDay { get; set; }
    public string TechStopNumber { get; set; }
    //public List<string> LocationId { get; set; }
}

public class ProductFacilitiesType2Model
{
    public string LastSeatAvailable { get; set; }
    public string LevelOfAccess { get; set; }
    public string ElectronicTicketing { get; set; }
    public string OperationalSuffix { get; set; }
    public string ProductDetailQualifier { get; set; }
    //public List<string> FlightCharacteristic { get; set; }
}

public class ProductDateTimeType8Model
{
    //public string TypeOfAircraft { get; set; }
    public string ArrivalDate { get; set; }
    public string ArrivalTime { get; set; }
    public string DepartureDate { get; set; }
    public string DepartureTime { get; set; }
    public string Duration { get; set; }
}

and these are my viewModels

    public class SearchReplyModel
{
    public SearchReplyModel()
    {
        Flights = new List<Flights>();
    }
    public List<Flights> Flights { get; set; }
}

public class Flights
{
    public Flights()
    {
        GroupOfFlights = new List<GroupOfFlights>();
    }
    public List<GroupOfFlights> GroupOfFlights { get; set; }
}

public class GroupOfFlights
{
    public GroupOfFlights()
    {
        FlightDetails = new List<FlightDetails>();
    }
    public string RefNumber { get; set; }
    public string Duration { get; set; }
    public List<FlightDetails> FlightDetails { get; set; }
}

public class FlightDetails
{
    public string ArrivalDate { get; set; }
    public string ArrivalTime { get; set; }
    public string DepartureDate { get; set; }
    public string DepartureTime { get; set; }
    public string Duration { get; set; }
    public string FromCity { get; set; }
    public string ToCity { get; set; }
    public string Terminal { get; set; }
    public string FlightNumber { get; set; }
    public string FlightName { get; set; }
    public string ElectronicTicketing { get; set; }
    public string EquipmentType { get; set; }
}

And i tried this..

Mapper.CreateMap<ProductDateTimeType8Model,Centra.Core.Model.Fare.FlightDetails>().ForMember(x=>x.ArrivalDate,c=>c.MapFrom(p=>p.ArrivalDate));
Mapper.CreateMap<ProductDateTimeType8Model,Centra.Core.Model.Fare.FlightDetails>().ForMember(x => x.ArrivalTime, c => c.MapFrom(p => p.ArrivalTime));

Mapper.CreateMap<ProductDateTimeType8Model,Centra.Core.Model.Fare.FlightDetails>().ForMember(x => x.DepartureDate, c => c.MapFrom(p => p.DepartureDate));

Mapper.CreateMap<ProductDateTimeType8Model,Centra.Core.Model.Fare.FlightDetails().ForMember(x => x.DepartureTime, c => c.MapFrom(p => p.DepartureTime));

Mapper.CreateMap<ProductDateTimeType8Model,Centra.Core.Model.Fare.FlightDetails().ForMember(x => x.Duration, c => c.MapFrom(p => p.Duration));

Mapper.CreateMap<MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsFlightDetailsModel, Centra.Core.Model.Fare.FlightDetails>();
Mapper.CreateMap<MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsModel, Centra.Core.Model.Fare.GroupOfFlights>();

Mapper.CreateMap<MasterPricerTravelBoardSearchReplyFlightIndexModel, Flights>();

Mapper.CreateMap<MasterPricerTravelBoardSearchReplyModel, SearchReplyModel>().ForMember(x => x.Flights, c => c.MapFrom(p => p.FlightIndex));

var jsonResult = new JsonResult() { Data=response.Content.ReadAsAsync<Response>().Result };
SearchReplyModel searchReplyModel=Mapper.Map<SearchReplyModel(jsonResult.Data);

When i try to Automap FlightDetails, i am getting all null values and is their any alternative way to do it? i am not aware of how to implement this in ValueResolver,can anyBody please help me out to rectify this!


Solution

  • Here is an example of mapping some of the other properties where it can be figured out with what you posted:

    Mapper.CreateMap<ProductDateTimeType8Model, FlightDetails>()
        .ForMember(x => x.ArrivalDate, c => c.MapFrom(p => p.ArrivalDate))
        .ForMember(x => x.ArrivalTime, c => c.MapFrom(p => p.ArrivalTime))
        .ForMember(x => x.DepartureDate, c => c.MapFrom(p => p.DepartureDate))
        .ForMember(x => x.DepartureTime, c => c.MapFrom(p => p.DepartureTime))
        .ForMember(x => x.Duration, c => c.MapFrom(p => p.Duration));
    
    Mapper.CreateMap<MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsFlightDetailsModel, FlightDetails>()
        .ForMember(x => x.FlightName, c => c.MapFrom(p => p.FlightInformation.FlightName))
        .ForMember(x => x.FlightNumber, c => c.MapFrom(p => p.FlightInformation.FlightOrtrainNumber));
    
    Mapper.CreateMap<MasterPricerTravelBoardSearchReplyModel, SearchReplyModel>()
        .ForMember(x => x.Flights, c => c.MapFrom(p => p.FlightIndex));
    

    The Mapper.CreateMap just defines the mapping, calling the Mapper.Map will map the object when called. So you could do something along the lines of

    var myInitialModel = new MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsFlightDetailsModel();
    var myDateTiomeModel = new ProductDateTimeType8Model();
    
    FlightDetails myResult = Mapper.Map(myInitialModel, new FlightDetails());
    myResult = Mapper.Map(myDateTiomeModel, myResult);
    

    I hope this gets you on the right track. After this example myResult should have FlightName and FlightNumnber filled in as well as the initial ones.