3

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"
        }
    }
}
Lex
  • 6,758
  • 2
  • 27
  • 42
user3276940
  • 487
  • 5
  • 16
  • Did you check the info you get when doing a query and the info you get in your angular post are the same format? Can you post an example of both? I had a similar problem there. – r007 Mar 22 '16 at 16:41
  • well, this is how I am setting my ng-models : indata.im.EventList[0].TotalAdults – user3276940 Mar 22 '16 at 20:26

1 Answers1

3

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.

wero
  • 32,544
  • 3
  • 59
  • 84