views:

454

answers:

1

Hello, I have a problem passing in a complex type into a controller.

Here is how my Model looks like:

public class Party
{
    [XmlAttribute]
    public int RsvpCode { get; set; }
    public List<Guest> Guests { get; set; }
    public string Comments { get; set; }
}

public class Guest
{
    [XmlAttribute]
    public string Name { get; set; }
    [XmlAttribute]
    public int MealOption { get; set; }
    [XmlAttribute]
    public bool Attending { get; set; }
}

My controller method looks like this:

    public JsonResult Submit(Party party)
    {
        return Json(party);
    }

and I'm trying to doing my ajax call like this:

var party = { RsvpCode: 1, Guests: [{ Name: "test asdfasdf", MealOption: 1, Attending: true }, { Name: "test asdfasdf", MealOption: 1, Attending: true}] };

                $.getJSON("/Rsvp/Submit", party, function(response) {
                    alert(response);
                });

Something's going wrong but I'm not sure what. I'm getting anything returned to me in the alert statement. Any ideas?

I've also tried looking at the value that's being submitted into the controller's method and it doesn't look right. I'm losing the information somewhere in the ajax call.

I also tried this creating my party object like this and it didn't work:

var party = { "RsvpCode": 1, "Guests": [{ "Name": "test asdfasdf", "MealOption": 1, "Attending": true }, { "Name": "test asdfasdf", "MealOption": 1, "Attending": true}], "Comments": "testdsfsdf" };
+2  A: 

When I was constructing the party variable, ASP.NET MVC expects it to look like this:

var party = { "RsvpCode": 1, "Guests[0].Name": "test asdfasdf", "Guests[0].MealOption": 1, "Guests[0].Attending": true, "Guests[1].Name": "test asdfasdf", "Guests[1].MealOption": 1, "Guests[1].Attending": true, "Comments": "testdsfsdf" };

ajma