ViewBag List Into ASP.NET MVC View

Mar 13 2018 5:37 AM
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:**
<Availability>
<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:**
<Availability>
<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 i.e Index Page. I get difficulty passing the Viewbag contents to View i.e Index. My code is not workable in View. I tried a lot. Please help me guys.

Answers (2)