Reputation: 487
I am working on an AngularJS App, and one of the methods in the service js post data to a web api with a following object structure in C#
public class InvitationModel
{
public string Name { get; set; }
public string Email { get; set; }
public EventModel[] EventList { get; set; }
}
public class EventModel
{
public string EventName { get; set; }
public int TotalAdults { get; set; }
public int TotalChildren { get; set; }
public bool IsAccepted { get; set; }
}
Problem is that when I post this data to a WEBAPI method, my parent level properties serializes correctly except the one that holds the collection. It gets set to null always.
The web API method that recieves the request is :
[AllowAnonymous]
[Route("RSVP")]
[HttpPost]
public bool Submit(InvitationModel invitationModel)
{
return true;
}
So, Name and Email serialize correctly, but EventList is NULL
I did check on the javascript side, and my js object holds both the array and other primitive properties. Issue I guess is at the .NET WebAPI side.
Request payload that gets posted is something like this :
{ "Name":"John Doe",
"EventList":{
"0":{ "TotalAdults":"1",
"TotalChildren":"2",
"EventName":"Event 1 Name"
},
"1":{ "TotalChildren":"2",
"TotalAdults":"2",
"EventName":"Event 2 Name"
},
"2":{ "TotalAdults":"1",
"TotalChildren":"1",
"EventName":"Event 3 Name"
}
}
}
Upvotes: 3
Views: 666
Reputation: 32980
The EventList
in your JSON is an object with properties "0"
, "1
", etc.
I guess it should be a JSON array, i.e.
{
"Name":"John Doe",
"EventList": [
{"TotalAdults":"1","TotalChildren":"2","EventName":"Event 1 Name"},
{"TotalChildren":"2","TotalAdults":"2","EventName":"Event 2 Name"},
...
], ...
to be correctly read into your C# eventlist property.
Upvotes: 3