使用 autoMapper 映射时获取空值

Getting null values when i map using autoMapper

这些是我的属性模型

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

这些是我的视图模型

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

我试过了..

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

当我尝试使用 Automap FlightDetails 时,我得到了所有空值,他们是否有其他方法可以做到这一点? 我不知道如何在 ValueResolver 中实现这个,任何人都可以帮助我纠正这个问题!

这是映射其他一些属性的示例,您可以在其中找到您发布的内容:

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

Mapper.CreateMap只是定义映射,调用Mapper.Map会在调用时映射对象。所以你可以按照

的方式做一些事情
var myInitialModel = new MasterPricerTravelBoardSearchReplyFlightIndexGroupOfFlightsFlightDetailsModel();
var myDateTiomeModel = new ProductDateTimeType8Model();

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

希望这能让您走上正轨。在这个例子之后 myResult 应该填写 FlightNameFlightNumnber 以及初始的