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.16
The API format means the the Segments
property must be a collection of otherType
(your model only has a single object). In addition the Sources
property is also a collection of string
.
Change you model to
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; }
}
and then assuming your only wanting to edit one Segments
and one Sources
, then in the GET method, add one object to each collection
SearchForFlight model = new SearchForFlight();
model.Segments.Add(new otherType());
model.Sources.Add(string.Empty);
....
return View(model);
and in the view, use for
loops to generate the html for the collections
@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
}
Note that this will generate id
attributes such as id="Segments_0__Origin"
so your script would need to be
Segments:
{
Origin: $("#Segments_0__Origin").val(),
Destination: $("#Segments_0__Destination").val(),
....
however there is no need to generate you javascript object manually, and your ajax can be simply
$.ajax({
....
data: $('form').serialize(),
....
and do not set the contentType
option so it uses the default application/x-www-form-urlencoded; charset=UTF-8