如何在自定义值解析器中对象内的列表进行操作

时间:2015-08-06 11:20:27

标签: asp.net-mvc linq automapper resolver

这是我的观点模型

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

这是我的mvc控制器和自定义值解析器

  public async Task<JsonResult> PostDatatoApi<Request, Response>(Request data, string apiUrl)
    {
        using (var client = new HttpClient())
        {
            SetupRestClient(client);
            client.BaseAddress = new Uri(centraBaseUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
            HttpResponseMessage response = await client.PostAsJsonAsync(client.BaseAddress + apiUrl, data);
            if (response.IsSuccessStatusCode)
            {
                var jsonResult = new JsonResult() { Data = response.Content.ReadAsAsync<Response>().Result };

                MasterPricerTravelBoardSearchReplyModel masterPricerTravelBoardSearchReplyModel = jsonResult.Data as MasterPricerTravelBoardSearchReplyModel;

                Mapper.CreateMap<MasterPricerTravelBoardSearchReplyModel, SearchReplyModel>().ForMember(x => x.Flights, op => op.ResolveUsing<CustomResolver1>());

                SearchReplyModel myResult = Mapper.Map<MasterPricerTravelBoardSearchReplyModel, SearchReplyModel>(masterPricerTravelBoardSearchReplyModel);
                return jsonResult;
            }
            throw new Exception("Invalid");
        }
    }

    public class CustomResolver1 : ValueResolver<MasterPricerTravelBoardSearchReplyModel, List<Flights>>
    {
        protected override List<Flights> ResolveCore(MasterPricerTravelBoardSearchReplyModel source)
        {
            List<Flights> response = new List<Flights>();
            Flights result = new Flights();
            result = new Flights()
            {
                GroupOfFlights = new List<GroupOfFlights>() { 

                }
                //{

                //    new GroupOfFlights()
                //    {
                //        RefNumber=source.FlightIndex[0].GroupOfFlights[0].PropFlightGrDetail.FlightProposal[0].Ref,
                //      FlightDetails = new List<Centra.Core.Model.Fare.FlightDetails>()
                //      {
                //          new Centra.Core.Model.Fare.FlightDetails()
                //          {
                //              ArrivalTime=source.FlightIndex[0].GroupOfFlights[0].FlightDetails[0].FlightInformation.ProductDateTime.ArrivalTime,
                //              DepartureDate=source.FlightIndex[0].GroupOfFlights[0].FlightDetails[0].FlightInformation.ProductDateTime.DepartureDate,
                //              DepartureTime=source.FlightIndex[0].GroupOfFlights[0].FlightDetails[0].FlightInformation.ProductDateTime.DepartureTime
                //          }
                //      }
                //    }



                //},


            };
            response.Add(result);
            return response;
        }

    }

在jsonResult中我得到了groupOfFlights的列表,并且我希望将groupOfFlights用于获取ArrivalDate,ArrivalTime,... groupOfFlights-->FlightDetials[0]-->FlightInformation-->ProductDateTime-->ArrivalDate中存在的对象,我只想说明groupOfFlights和那个FlightDetails这就是为什么我把它保持为FlightDetials[0],有没有人得到我?我知道这非常令人困惑......

0 个答案:

没有答案