在C#中用AJAX调用打印IList对象值

时间:2017-08-31 12:37:31

标签: jquery asp.net-mvc asp.net-web-api asp.net-mvc-5

我正在使用QPXExpress webservice API进行航班预订并通过ajax调用打印JSON结果,但无法在函数中打印IList对象值。下面是代码。我尝试使用foreach循环,但它不起作用。

尝试使用调试器并在警报功能中发布,但它没有显示结果。

模型类

flightrecord.cs

public class flightrecord
{

    private IList<CityData> city1;
    private IList<CityData> city2;
    private IList<AirportData> airport;
    private IList<TaxData> tax;

    public IList<CityData> City1
    {
        get
        {
            return city1;
        }

        set
        {
            city1 = value;
        }
    }

    public IList<CityData> City2
    {
        get
        {
            return city2;
        }

        set
        {
            city2 = value;
        }
    }

    public IList<AirportData> Airport
    {
        get
        {
            return airport;
        }

        set
        {
            airport = value;
        }
    }

    public IList<TaxData> Tax
    {
        get
        {
            return tax;
        }

        set
        {
            tax = value;
        }
    }

    public flightrecord()
    {

    }
    public flightrecord(IList<CityData> city1, IList<CityData> city2, IList<AirportData> airport, IList<TaxData> tax)
    {
        this.City1 = city1;
        this.City2 = city2;
        this.Airport = airport;
        this.Tax = tax;
    }
}

控制器类

public ActionResult flights()
{
    QPXExpressService service = new QPXExpressService(new BaseClientService.Initializer()
    {
        ApiKey = "AIzaSyCMy1cF1_QPOVFXY5e6W6lD_o3ewv4AUNk",
        ApplicationName = "TripAdvisor",
    });

    TripsSearchRequest x = new TripsSearchRequest();
    x.Request = new TripOptionsRequest();
    x.Request.Passengers = new PassengerCounts { AdultCount = 2 };
    x.Request.Slice = new List<SliceInput>();

    x.Request.Slice.Add(new SliceInput()
    {
        Origin = "KHI",
        Destination = "LHR",
        Date = "2017-09-8"
    });
    x.Request.Solutions = 10;
    var result = service.Trips.Search(x).Execute();

    //List<flightrecord> record = new List<flightrecord>();
    //record.Add(new flightrecord(result.Trips.Data.City, result.Trips.Data.City, result.Trips.Data.Airport, result.Trips.Data.Tax));

    flightrecord record = new flightrecord(result.Trips.Data.City, result.Trips.Data.City, result.Trips.Data.Airport, result.Trips.Data.Tax);

    //   ViewBag.listflights = result;

    return Json(record, JsonRequestBehavior.AllowGet);
}

查看

<script type="text/javascript">

$(document).ready(function () {
    $("#flights").click(function (e) {
        e.preventDefault();
        $.ajax({
            url: "/Hotel/flights",
            type: 'GET',
            datatype: 'json',
            contentType: 'application/json',
            data: {},
            success: function (data) {
                //debugger;

                //alert(data.City1.Name);
                var s = '';
                for (var i in data.City1) {
                    s += data[i].City1.Name;
                }
                //var s = '';

                //s += 'city1: ' + data[i].City1.Name + '<br>city2: ' + data[i].City2.Name + '<br>airport :' + data[i].Airport.Name + '<br>tax: ' + data[i].Tax;
                //    $("#box").html('');
                $("#box1").html(s);


            }
        });
    });
});
</script>

0 个答案:

没有答案