简体   繁体   中英

Deserialized C# into ASP.NET MVC View

I have deserialized the XML Response from API, now I need to display the deserialized objects to list in ASP.NET MVC View.

XML Response:

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

Controller:

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

    }

and the Model for class.

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

Model for FlightAvailibility :

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

Controller:

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

I want to show the availability List In Index View: Index.cshtml I have deserialized the XML Response from API, now I need to display the deserialized objects to list in ASP.NET MVC View.

XML Response:

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

Controller:

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

    }

and the Model for class.

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

Model for FlightAvailibility :

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

Controller:

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

I want to show the availability List In Index 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>

I need to represent the list of availability: AvailabilityOutbound & AvailabilityReturn using Viewbag in Asp.Net MVC View ie Index Page. I get difficulty passing the Viewbag contents to View ie Index. My code is not workable in View. I tried a lot. Please help me guys.

First you get you data from api in xml format and then JavaScriptSerializer and make class to pass data to model.In this code the rentlisting action get list from api and show on the view pass list to the view in GetRentListing() function get the api data and last class show they convert the api data to class object and show in view model.

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


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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