我已经从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; }
}
}
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; }
}
控制器:
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; }
}
}
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; }
}
控制器:
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>
我需要代表可用性列表:AvailabilityOutbound&amp;可用性返回使用Asp.Net MVC中的Viewbag查看索引页面。我很难将Viewbag内容传递给View,Index。我的代码在View中无法使用。我尝试了很多。请帮帮我们。
答案 0 :(得分:0)
首先,您从api以xml格式获取数据,然后使用JavaScriptSerializer并使类将数据传递给模型。在此代码中,租借列表从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; }
}