I am sending data to a rest API of a travel portal. According to their documentation, the json data must be in the following format
{
"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"
]
}
My models are
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; }
}
The following code populates my model correctly, but if I include the square brackets, binding fails.
<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(),
}
};
Controller
[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())
{
.....
}
}
How do I generate the correct format and bind to the model?
Segmentsis an object (typeofotherType) not a collection of objects) so you cannot generate a collection using[ ... ]. What to you mean the according to documentation? Are you posting to an API that requires the propertySegmentsto be a collection? (in which case you need to change your model)public IEnumerable<otherType> Segments { get; set; }and use the code in your first snippet. Of course this would all be far easier if you just used$('form').serialize();rather than manually building your object.