如何在web api控制器中的json嵌套对象中发送数组括号

我将数据发送到旅行门户的其他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(); FlightCabinClassList = new List(); 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 FlightCabinClassList { get; set; } [JsonIgnore] public IEnumerable JourneyList { get; set; } public string Sources { get; set; } } 

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

  $(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 SearchFlight([FromBody]SearchForFlight sof) { string url = "http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/Search/"; using (HttpClient client = new HttpClient()) { ..... } } 

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

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

将模型更改为

 public class SearchForFlight { public SearchForFlight() { Segments = new List(); Sources = new List(); } .... public string PreferedLines { get; set; } public List Segments { get; set; } [JsonIgnore] public IEnumerable FlightCabinClassList { get; set; } [JsonIgnore] public IEnumerable JourneyList { get; set; } public List Sources { get; set; } } 

然后假设您只想编辑一个Segments和一个Sources ,然后在GET方法中,为每个集合添加一个对象

 SearchForFlight model = new SearchForFlight(); model.Segments.Add(new otherType()); model.Sources.Add(string.Empty); .... return View(model); 

并在视图中,使用for循环生成集合的html

 @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"因此您的脚本需要

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

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

 $.ajax({ .... data: $('form').serialize(), .... 

并且不要设置contentType选项,因此它使用默认的application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8