1

我在将复杂类型传递给控制器​​时遇到问题。

这是我的模型的样子:

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; }
}

我的控制器方法如下所示:

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

我正在尝试像这样进行我的 ajax 调用:

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);
                });

出了点问题,但我不确定是什么。我在警报声明中收到任何返回给我的东西。有任何想法吗?

我还尝试查看提交到控制器方法中的值,但它看起来不正确。我在 ajax 调用中的某处丢失了信息。

我也试过这样创建我的派对对象,但它没有用:

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

1 回答 1

2

当我构造party 变量时,ASP.NET MVC 期望它看起来像这样:

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" };

于 2009-03-19T04:05:24.787 回答