複雑な型をコントローラーの問題に渡すASP.NET MVC
-
03-07-2019 - |
質問
複雑な型をコントローラーに渡す際に問題があります。
モデルは次のようになります。
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" };
解決
パーティー変数を作成していたとき、ASP.NET MVCはそれが次のようになることを期待しています:
var party = {&quot; RsvpCode&quot;:1、&quot; Guests [0] .Name&quot ;:&quot; test asdfasdf&quot;、&quot; Guests [0] .MealOption&quot;:1、&quot; Guests [0] .Attending&quot ;:true、&quot; Guests [1] .Name&quot ;:&quot; test asdfasdf&quot;、&quot; Guests [1] .MealOption&quot ;: 1、&quot; Guests [1] .Attending&quot ;: true、&quot; Comments&quot ;:&quot ; testdsfsdf&quot; };
所属していません StackOverflow