EmployeeMasterA
クラスにCards
というコレクションがあります。FormData
オブジェクトにコレクションを追加する正しい方法がわかりません。私はこのような何かしたい:JQuery FormDataオブジェクトにコレクションを追加する方法
var data = new FormData();
data.append('Cards[0].CardId', 1);
data.append('Cards[0].CardTitle', "Credit");
data.append('Cards[1].CardId', 2);
data.append('Cards[1].CardTitle', "Debit");
をし、これが私のAjaxリクエストです:
$.ajax({
url: '/EmployeeMasterA/Create',
data: data,
type: 'POST',
contentType: false,
processData : false,
success: function (result) {
if (result.Success == "1") {
}
}
});
と私のcontroller
:
[HttpPost]
public ActionResult Create(EmployeeMasterA employeeMasterA)
{
//get data and perform logics
}
EmployeeMasterA
クラス:
public class EmployeeMasterA
{
public string EmployeeCode { get; set; }
public virtual ICollection<Cards> Cards { get; set; }
}
EmployeeMasterA
は、カードのコレクションが含まれ、ここでCards
クラスは次のとおりです。
public class Cards
{
public int CardId { get; set; }
public string CardTitle { get; set; }
}
私はFormData
オブジェクトにCards
のコレクションを追加するにはどうすればよいですか?したがって、それはEmployeeMasterA
クラスと正しくバインドされます。
更新
私はデバッグモードで見て、Cards
コレクションはnull
で、なぜ?
あなたは完璧な作品を示していると 'Cards'が正しく装着されている2つの項目を含むコード:
はこれを参照してください。それがあなたのために働いていない場合、それはあなたのコードではなく、あなたはいくつかの関連するコードを省略しています。 –
@StephenMuecke、そうです、私は非常にばかげた間違いをしていました。FormDataが再初期化されていたので、私は 'null'を得ていました。ありがとうございます:) –