如何在web api controller

时间:2016-05-19 06:40:36

标签: jquery arrays json asp.net-mvc asp.net-web-api

我将数据发送到旅行门户网站的其他API。根据他们的文档,json数据必须采用以下格式

{
    "EndUserIp": "192.168.10.10",
    "TokenId": "ac2751e9-4cc3-406f-b678-c947e4f57a00",
    "AdultCount": "1",
    "ChildCount": "0",
    "InfantCount": "0",
    "DirectFlight": "false",
    "OneStopFlight": "false",
    "JourneyType": "1",
    "PreferredAirlines": null,
    "Segments": [
    {
        "Origin": "DEL",
        "Destination": "BOM",
        "FlightCabinClass": "1",
        "PreferredDepartureTime": "2015-11-06T00: 00: 00",
        "PreferredArrivalTime": "2015-11-06T00: 00: 00"
    }],
    "Sources": [
        "6E"
    ]
}

我的模特

public class otherType
{
    public string Origin { get; set; }
    public string Destination { get; set; }
    public FlightCabinClass FlightCabinClass { get; set; }
    [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime? PreferredDepartureTime { get; set; }
    [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime PreferredArrivalTime { get; set; }
}

public class SearchForFlight
{
    public SearchForFlight()
    {
        JourneyList = new List<SelectListItem>();
        FlightCabinClassList = new List<SelectListItem>();
        Segments = new otherType();
    }
    public string EndUserIp { get; set; }
    public string TokenId { get; set; }
    public int AdultCount { get; set; }
    public int ChildCount { get; set; }
    public int InfantCount { get; set; }
    public bool DirectFlight { get; set; }
    public bool OneStopFlight { get; set; }
    public JourneyType JourneyType { get; set; }
    public string PreferedLines { get; set; }
    public otherType Segments { get; set; }
    [JsonIgnore]
    public IEnumerable<SelectListItem> FlightCabinClassList { get; set; }
    [JsonIgnore]
    public IEnumerable<SelectListItem> JourneyList { get; set; }
    public string Sources { get; set; }
}

以下代码正确填充了我的模型,但如果我包含方括号,则绑定失败。

<script>
    $(document).ready(function () {                 
        $("#btnPost").click(function () {
            var sof = {
                AdultCount: $("#AdultCount").val(),
                JourneyType: $("#JourneyType :selected").text(),
                PreferredAirlines: null,
                Segments:
                {
                    Origin: $("#Segments_Origin").val(),
                    Destination: $("#Segments_Destination").val(),
                    FlightCabinClass: $("#FlightCabinClass").val(),
                    PreferredDepartureTime: $("#Segments_PreferredDepartureTime").val(),
                    PreferredArrivalTime: $("#Segments_PreferredArrivalTime").val(),
                }
            };

控制器

[System.Web.Http.HttpPost]
public async Task<HttpResponseMessage> SearchFlight([FromBody]SearchForFlight sof)
{
    string url = "http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/Search/";
    using (HttpClient client = new HttpClient())
    {
        .....
    }
}

如何生成正确的格式并绑定到模型?

1 个答案:

答案 0 :(得分:1)

API格式表示condition_variable属性必须是Segments的集合(您的模型只有一个对象)。此外,otherType属性也是Sources的集合。

将模型更改为

string

然后假设您只想编辑一个public class SearchForFlight { public SearchForFlight() { Segments = new List<otherType>(); Sources = new List<string>(); } .... public string PreferedLines { get; set; } public List<otherType> Segments { get; set; } [JsonIgnore] public IEnumerable<SelectListItem> FlightCabinClassList { get; set; } [JsonIgnore] public IEnumerable<SelectListItem> JourneyList { get; set; } public List<string> Sources { get; set; } } 和一个Segments,然后在GET方法中,为每个集合添加一个对象

Sources

并在视图中,使用SearchForFlight model = new SearchForFlight(); model.Segments.Add(new otherType()); model.Sources.Add(string.Empty); .... return View(model); 循环生成集合的html

for

请注意,这会生成@for(int i = 0; i < Model.Segments.Count; i++) { @Html.LabelFor(m => m.Segments[i].Origin) @Html.TextBoxFor(m => m.Segments[i].Origin) @Html.ValidationMesageFor(m => m.Segments[i].Origin) .... // other properties of otherType } 属性,例如id,因此您的脚本需要

id="Segments_0__Origin"

但是没有必要手动生成javascript对象,而你的ajax可以简单地

Segments:
{
    Origin: $("#Segments_0__Origin").val(),
    Destination: $("#Segments_0__Destination").val(),
    ....

并且不要设置$.ajax({ .... data: $('form').serialize(), .... 选项,因此它使用默认的contentType