繁体   English   中英

将C#反序列化为ASP.NET MVC视图

[英]Deserialized C# into ASP.NET MVC View

我已经反序列化了来自API的XML响应,现在我需要显示反序列化的对象以在ASP.NET MVC视图中列出。

XML回应:

<AvailabilityOutbound>...</AvailabilityOutbound>
<AvailabilityReturn>...</AvailabilityReturn>
</Availability>

控制器:

 public ActionResult Create([Bind(Include="FlightId,Origin,Destination,DepartFromDate,DepartToDate,ReturnFromDate,ReturnToDate,AdultPax,ChildPax,Language")] FlightAvailibility flightavailibility)
    {
        if (ModelState.IsValid)
        {
            BookingServiceReference.BookingService newBooking= new BookingServiceReference.BookingService();
            string xmlResult = string.Empty;
            xmlResult = newBooking.FlightAvailability("TESTAPI", "TESTAPI", flightavailibility.Origin, flightavailibility.Destination,
                flightavailibility.DepartFromDate, flightavailibility.DepartToDate, flightavailibility.ReturnFromDate, flightavailibility.ReturnToDate,
                flightavailibility.AdultPax, flightavailibility.ChildPax, 0, 0, null, null, null, "NP");
         //   return this.Content(xmlResult, "text/xml");
            XmlSerializer serializer = new XmlSerializer(typeof(Availability));
            using (StringReader reader = new StringReader(xmlResult))
            {
                Availability deserialized = (Availability)serializer.Deserialize(reader);

                ViewBag.NewList = deserialized;
                return View(deserialized);
            }
        }
        return RedirectToAction("Index");

    }

以及课堂模型

namespace YetiAirlinesProjectDev.Models
{
    [XmlRoot(ElementName = "AvailabilityFlight")]
    public class AvailabilityFlight
    {
        [XmlElement(ElementName = "airline_rcd")]
        public string Airline_rcd { get; set; }
        [XmlElement(ElementName = "flight_number")]
        public string Flight_number { get; set; }
        [XmlElement(ElementName = "booking_class_rcd")]
        public string Booking_class_rcd { get; set; }
        [XmlElement(ElementName = "boarding_class_rcd")]
        public string Boarding_class_rcd { get; set; }
        [XmlElement(ElementName = "flight_id")]
        public string Flight_id { get; set; }
        [XmlElement(ElementName = "origin_rcd")]
        public string Origin_rcd { get; set; }
        [XmlElement(ElementName = "destination_rcd")]
        public string Destination_rcd { get; set; }
        [XmlElement(ElementName = "origin_name")]
        public string Origin_name { get; set; }
        [XmlElement(ElementName = "destination_name")]
        public string Destination_name { get; set; }
        [XmlElement(ElementName = "flight_status_rcd")]
        public string Flight_status_rcd { get; set; }
        [XmlElement(ElementName = "departure_date")]
        public string Departure_date { get; set; }
        [XmlElement(ElementName = "planned_departure_time")]
        public string Planned_departure_time { get; set; }
        [XmlElement(ElementName = "planned_arrival_time")]
        public string Planned_arrival_time { get; set; }
        [XmlElement(ElementName = "fare_id")]
        public string Fare_id { get; set; }
        [XmlElement(ElementName = "fare_code")]
        public string Fare_code { get; set; }
        [XmlElement(ElementName = "transit_points")]
        public string Transit_points { get; set; }
        [XmlElement(ElementName = "transit_points_name")]
        public string Transit_points_name { get; set; }
        [XmlElement(ElementName = "transit_flight_id")]
        public string Transit_flight_id { get; set; }
        [XmlElement(ElementName = "transit_booking_class_rcd")]
        public string Transit_booking_class_rcd { get; set; }
        [XmlElement(ElementName = "transit_boarding_class_rcd")]
        public string Transit_boarding_class_rcd { get; set; }
        [XmlElement(ElementName = "transit_airport_rcd")]
        public string Transit_airport_rcd { get; set; }
        [XmlElement(ElementName = "transit_departure_date")]
        public string Transit_departure_date { get; set; }
        [XmlElement(ElementName = "transit_planned_departure_time")]
        public string Transit_planned_departure_time { get; set; }
        [XmlElement(ElementName = "transit_planned_arrival_time")]
        public string Transit_planned_arrival_time { get; set; }
        [XmlElement(ElementName = "transit_fare_id")]
        public string Transit_fare_id { get; set; }
        [XmlElement(ElementName = "transit_name")]
        public string Transit_name { get; set; }
        [XmlElement(ElementName = "transit_waitlist_open_flag")]
        public string Transit_waitlist_open_flag { get; set; }
        [XmlElement(ElementName = "transit_airline_rcd")]
        public string Transit_airline_rcd { get; set; }
        [XmlElement(ElementName = "transit_flight_number")]
        public string Transit_flight_number { get; set; }
        [XmlElement(ElementName = "transit_flight_status_rcd")]
        public string Transit_flight_status_rcd { get; set; }
        [XmlElement(ElementName = "transit_flight_duration")]
        public string Transit_flight_duration { get; set; }
        [XmlElement(ElementName = "transit_class_open_flag")]
        public string Transit_class_open_flag { get; set; }
        [XmlElement(ElementName = "transit_nesting_string")]
        public string Transit_nesting_string { get; set; }
        [XmlElement(ElementName = "nesting_string")]
        public string Nesting_string { get; set; }
        [XmlElement(ElementName = "full_flight_flag")]
        public string Full_flight_flag { get; set; }
        [XmlElement(ElementName = "class_open_flag")]
        public string Class_open_flag { get; set; }
        [XmlElement(ElementName = "close_web_sales")]
        public string Close_web_sales { get; set; }
        [XmlElement(ElementName = "total_adult_fare")]
        public string Total_adult_fare { get; set; }
        [XmlElement(ElementName = "total_child_fare")]
        public string Total_child_fare { get; set; }
        [XmlElement(ElementName = "total_infant_fare")]
        public string Total_infant_fare { get; set; }
        [XmlElement(ElementName = "fare_column")]
        public string Fare_column { get; set; }
        [XmlElement(ElementName = "flight_comment")]
        public string Flight_comment { get; set; }
        [XmlElement(ElementName = "filter_logic_flag")]
        public string Filter_logic_flag { get; set; }
        [XmlElement(ElementName = "restriction_text")]
        public string Restriction_text { get; set; }
        [XmlElement(ElementName = "flight_duration")]
        public string Flight_duration { get; set; }
        [XmlElement(ElementName = "class_capacity")]
        public string Class_capacity { get; set; }
        [XmlElement(ElementName = "waitlist_open_flag")]
        public string Waitlist_open_flag { get; set; }
        [XmlElement(ElementName = "refundable_flag")]
        public string Refundable_flag { get; set; }
        [XmlElement(ElementName = "currency_rcd")]
        public string Currency_rcd { get; set; }
        [XmlElement(ElementName = "aircraft_type_rcd")]
        public string Aircraft_type_rcd { get; set; }
        [XmlElement(ElementName = "transit_aircraft_type_rcd")]
        public string Transit_aircraft_type_rcd { get; set; }
        [XmlElement(ElementName = "arrival_date")]
        public string Arrival_date { get; set; }
        [XmlElement(ElementName = "transit_arrival_date")]
        public string Transit_arrival_date { get; set; }
        [XmlElement(ElementName = "number_of_stops")]
        public string Number_of_stops { get; set; }
        [XmlElement(ElementName = "eticket_flag")]
        public string Eticket_flag { get; set; }
        [XmlElement(ElementName = "nest_seat_availabile")]
        public string Nest_seat_availabile { get; set; }
        [XmlElement(ElementName = "endorsement_text")]
        public string Endorsement_text { get; set; }
    }

    [XmlRoot(ElementName = "AvailabilityOutbound")]
    public class AvailabilityOutbound
    {
        [XmlElement(ElementName = "AvailabilityFlight")]
        public List<AvailabilityFlight> AvailabilityFlight { get; set; }
    }

    [XmlRoot(ElementName = "AvailabilityReturn")]
    public class AvailabilityReturn
    {
        [XmlElement(ElementName = "AvailabilityFlight")]
        public List<AvailabilityFlight> AvailabilityFlight { get; set; }
    }

    [XmlRoot(ElementName = "Availability")]
    public class Availability
    {
        [XmlElement(ElementName = "AvailabilityOutbound")]
        public AvailabilityOutbound AvailabilityOutbound { get; set; }
        [XmlElement(ElementName = "AvailabilityReturn")]
        public AvailabilityReturn AvailabilityReturn { get; set; }
    }
}

FlightAvailability模型:

public class FlightAvailibility
{

    public int FlightId { get; set; }
    public string Origin { get; set; }
    public string Destination { get; set; }
    public string DepartFromDate { get; set; }
    public string DepartToDate { get; set; }
    public string ReturnFromDate { get; set; }
    public string ReturnToDate { get; set; }
    public short AdultPax { get; set; }
    public short ChildPax { get; set; }
    public string Language { get; set; }
}

控制器:

  public ActionResult Index()
    {
        return View();
    }

我想在索引视图中显示可用性列表: Index.cshtml我已经反序列化了来自API的XML响应,现在我需要显示反序列化的对象以在ASP.NET MVC视图中列出。

XML回应:

<AvailabilityOutbound>...</AvailabilityOutbound>
<AvailabilityReturn>...</AvailabilityReturn>
</Availability>

控制器:

 public ActionResult Create([Bind(Include="FlightId,Origin,Destination,DepartFromDate,DepartToDate,ReturnFromDate,ReturnToDate,AdultPax,ChildPax,Language")] FlightAvailibility flightavailibility)
    {
        if (ModelState.IsValid)
        {
            BookingServiceReference.BookingService newBooking= new BookingServiceReference.BookingService();
            string xmlResult = string.Empty;
            xmlResult = newBooking.FlightAvailability("TESTAPI", "TESTAPI", flightavailibility.Origin, flightavailibility.Destination,
                flightavailibility.DepartFromDate, flightavailibility.DepartToDate, flightavailibility.ReturnFromDate, flightavailibility.ReturnToDate,
                flightavailibility.AdultPax, flightavailibility.ChildPax, 0, 0, null, null, null, "NP");
         //   return this.Content(xmlResult, "text/xml");
            XmlSerializer serializer = new XmlSerializer(typeof(Availability));
            using (StringReader reader = new StringReader(xmlResult))
            {
                Availability deserialized = (Availability)serializer.Deserialize(reader);

                ViewBag.NewList = deserialized;
                return View(deserialized);
            }
        }
        return RedirectToAction("Index");

    }

以及课堂模型

namespace YetiAirlinesProjectDev.Models
{
    [XmlRoot(ElementName = "AvailabilityFlight")]
    public class AvailabilityFlight
    {
        [XmlElement(ElementName = "airline_rcd")]
        public string Airline_rcd { get; set; }
        [XmlElement(ElementName = "flight_number")]
        public string Flight_number { get; set; }
        [XmlElement(ElementName = "booking_class_rcd")]
        public string Booking_class_rcd { get; set; }
        [XmlElement(ElementName = "boarding_class_rcd")]
        public string Boarding_class_rcd { get; set; }
        [XmlElement(ElementName = "flight_id")]
        public string Flight_id { get; set; }
        [XmlElement(ElementName = "origin_rcd")]
        public string Origin_rcd { get; set; }
        [XmlElement(ElementName = "destination_rcd")]
        public string Destination_rcd { get; set; }
        [XmlElement(ElementName = "origin_name")]
        public string Origin_name { get; set; }
        [XmlElement(ElementName = "destination_name")]
        public string Destination_name { get; set; }
        [XmlElement(ElementName = "flight_status_rcd")]
        public string Flight_status_rcd { get; set; }
        [XmlElement(ElementName = "departure_date")]
        public string Departure_date { get; set; }
        [XmlElement(ElementName = "planned_departure_time")]
        public string Planned_departure_time { get; set; }
        [XmlElement(ElementName = "planned_arrival_time")]
        public string Planned_arrival_time { get; set; }
        [XmlElement(ElementName = "fare_id")]
        public string Fare_id { get; set; }
        [XmlElement(ElementName = "fare_code")]
        public string Fare_code { get; set; }
        [XmlElement(ElementName = "transit_points")]
        public string Transit_points { get; set; }
        [XmlElement(ElementName = "transit_points_name")]
        public string Transit_points_name { get; set; }
        [XmlElement(ElementName = "transit_flight_id")]
        public string Transit_flight_id { get; set; }
        [XmlElement(ElementName = "transit_booking_class_rcd")]
        public string Transit_booking_class_rcd { get; set; }
        [XmlElement(ElementName = "transit_boarding_class_rcd")]
        public string Transit_boarding_class_rcd { get; set; }
        [XmlElement(ElementName = "transit_airport_rcd")]
        public string Transit_airport_rcd { get; set; }
        [XmlElement(ElementName = "transit_departure_date")]
        public string Transit_departure_date { get; set; }
        [XmlElement(ElementName = "transit_planned_departure_time")]
        public string Transit_planned_departure_time { get; set; }
        [XmlElement(ElementName = "transit_planned_arrival_time")]
        public string Transit_planned_arrival_time { get; set; }
        [XmlElement(ElementName = "transit_fare_id")]
        public string Transit_fare_id { get; set; }
        [XmlElement(ElementName = "transit_name")]
        public string Transit_name { get; set; }
        [XmlElement(ElementName = "transit_waitlist_open_flag")]
        public string Transit_waitlist_open_flag { get; set; }
        [XmlElement(ElementName = "transit_airline_rcd")]
        public string Transit_airline_rcd { get; set; }
        [XmlElement(ElementName = "transit_flight_number")]
        public string Transit_flight_number { get; set; }
        [XmlElement(ElementName = "transit_flight_status_rcd")]
        public string Transit_flight_status_rcd { get; set; }
        [XmlElement(ElementName = "transit_flight_duration")]
        public string Transit_flight_duration { get; set; }
        [XmlElement(ElementName = "transit_class_open_flag")]
        public string Transit_class_open_flag { get; set; }
        [XmlElement(ElementName = "transit_nesting_string")]
        public string Transit_nesting_string { get; set; }
        [XmlElement(ElementName = "nesting_string")]
        public string Nesting_string { get; set; }
        [XmlElement(ElementName = "full_flight_flag")]
        public string Full_flight_flag { get; set; }
        [XmlElement(ElementName = "class_open_flag")]
        public string Class_open_flag { get; set; }
        [XmlElement(ElementName = "close_web_sales")]
        public string Close_web_sales { get; set; }
        [XmlElement(ElementName = "total_adult_fare")]
        public string Total_adult_fare { get; set; }
        [XmlElement(ElementName = "total_child_fare")]
        public string Total_child_fare { get; set; }
        [XmlElement(ElementName = "total_infant_fare")]
        public string Total_infant_fare { get; set; }
        [XmlElement(ElementName = "fare_column")]
        public string Fare_column { get; set; }
        [XmlElement(ElementName = "flight_comment")]
        public string Flight_comment { get; set; }
        [XmlElement(ElementName = "filter_logic_flag")]
        public string Filter_logic_flag { get; set; }
        [XmlElement(ElementName = "restriction_text")]
        public string Restriction_text { get; set; }
        [XmlElement(ElementName = "flight_duration")]
        public string Flight_duration { get; set; }
        [XmlElement(ElementName = "class_capacity")]
        public string Class_capacity { get; set; }
        [XmlElement(ElementName = "waitlist_open_flag")]
        public string Waitlist_open_flag { get; set; }
        [XmlElement(ElementName = "refundable_flag")]
        public string Refundable_flag { get; set; }
        [XmlElement(ElementName = "currency_rcd")]
        public string Currency_rcd { get; set; }
        [XmlElement(ElementName = "aircraft_type_rcd")]
        public string Aircraft_type_rcd { get; set; }
        [XmlElement(ElementName = "transit_aircraft_type_rcd")]
        public string Transit_aircraft_type_rcd { get; set; }
        [XmlElement(ElementName = "arrival_date")]
        public string Arrival_date { get; set; }
        [XmlElement(ElementName = "transit_arrival_date")]
        public string Transit_arrival_date { get; set; }
        [XmlElement(ElementName = "number_of_stops")]
        public string Number_of_stops { get; set; }
        [XmlElement(ElementName = "eticket_flag")]
        public string Eticket_flag { get; set; }
        [XmlElement(ElementName = "nest_seat_availabile")]
        public string Nest_seat_availabile { get; set; }
        [XmlElement(ElementName = "endorsement_text")]
        public string Endorsement_text { get; set; }
    }

    [XmlRoot(ElementName = "AvailabilityOutbound")]
    public class AvailabilityOutbound
    {
        [XmlElement(ElementName = "AvailabilityFlight")]
        public List<AvailabilityFlight> AvailabilityFlight { get; set; }
    }

    [XmlRoot(ElementName = "AvailabilityReturn")]
    public class AvailabilityReturn
    {
        [XmlElement(ElementName = "AvailabilityFlight")]
        public List<AvailabilityFlight> AvailabilityFlight { get; set; }
    }

    [XmlRoot(ElementName = "Availability")]
    public class Availability
    {
        [XmlElement(ElementName = "AvailabilityOutbound")]
        public AvailabilityOutbound AvailabilityOutbound { get; set; }
        [XmlElement(ElementName = "AvailabilityReturn")]
        public AvailabilityReturn AvailabilityReturn { get; set; }
    }
}

FlightAvailability模型:

public class FlightAvailibility
{

    public int FlightId { get; set; }
    public string Origin { get; set; }
    public string Destination { get; set; }
    public string DepartFromDate { get; set; }
    public string DepartToDate { get; set; }
    public string ReturnFromDate { get; set; }
    public string ReturnToDate { get; set; }
    public short AdultPax { get; set; }
    public short ChildPax { get; set; }
    public string Language { get; set; }
}

控制器:

  public ActionResult Index()
    {
        return View();
    }

我想在索引视图中显示可用性列表: Index.cshtml

@model {YetiAirlinesProjectDev.Models.Availability objnew = (YetiAirlinesProjectDev.Models.Availability)ViewBag.NewList}

    <table class="table">
        <tr>
            <th>
                @Html.Display("Aircraft Name")
            </th>
        </tr>

        @foreach (var item in objnew) {
            <tr>

                <td>
                   @item
                </td>

            </tr>
    }

    </table>

我需要代表可用性的列表:在Asp.Net MVC视图(即索引页)中使用Viewbag的AvailabilityOutbound和AvailabilityReturn。 我很难将Viewbag的内容传递给View即Index。 我的代码在View中不起作用。 我试了很多 请帮助我。

首先以xml格式从api获取数据,然后是JavaScriptSerializer并让类将数据传递给模型。在此代码中,rentlisting操作从api获取列表并在视图传递列表上显示给GetRentListing()函数中的视图以获取api数据和上一类显示它们将api数据转换为类对象并在视图模型中显示。

    public ActionResult RentListing()
    {
        if (Session["rentlist"] != null)
        {
            var list = (List<RootObject>)Session["rentlist"];
            return View(list);
        }
        else
        {
            var value = GetRentListing();
            var list = (List<RootObject>)Session["rentlist"];
            return View(list);
        }
    }
     [HttpGet]
    public async System.Threading.Tasks.Task<string> GetRentListing()
    {
        try
        {
            HttpClient client = new HttpClient();
            string AccessCode = ConfigurationManager.AppSettings["AccessCode"];
            string GroupCode = ConfigurationManager.AppSettings["GroupCode"];
            string Url = "http://www.airlist.com/v1.1/website.asmx/RentListings?AccessCode=" + AccessCode + "&GroupCode=" + GroupCode + "&unitId=" + "" + "&StartPriceRange=" + "" + "&EndPriceRange=" + "" + "&floorAreaMin=" + "" + "&floorAreaMax=" + "" + "&cityId=" + "" + "&unitTypeId=" + "" + "&BedroomsMin=" + "" + "&BedroomsMax=" + "" + "&CommunityId=" + "" + "";
            client.BaseAddress = new Uri(Url);
            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = client.GetAsync(Url).Result;
            string data = "false";
            if (response.IsSuccessStatusCode)
            {
                data = await response.Content.ReadAsStringAsync();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(data);
                string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);

                var list = new JavaScriptSerializer().Deserialize<dynamic>(json);
                var d = ((list["RentListings"])["RentListing"]);
                var lists = new JavaScriptSerializer().Serialize(d);
                lists = lists.Replace("#cdata-section", "cdata");
                var listss = new JavaScriptSerializer().Deserialize<List<RootObject>>(lists);

                Session["rentlist"] = listss;

            }
            return data;
        }
        catch (Exception e)
        {

            throw;
        }
    }
public class RootObject
{
    public string Count { get; set; }
    public string Country { get; set; }
    public object State { get; set; }
    public string City { get; set; }
    public object District { get; set; }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM