使用autoMapper进行映射时获取空值

时间:2015-08-03 09:11:17

标签: asp.net-mvc asp.net-mvc-3 automapper automapping

这些是我的属性模型

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

这些是我的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; }
}

我试过这个..

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中实现这一点,anyBody请帮我解决这个问题!

1 个答案:

答案 0 :(得分:2)

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

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以及初始内容。