ネストされたビューモデル構造があります:
public class PersonViewModel
{
public int Height { get; set; }
public List<LegViewModel> Legs {get;set;}
}
public class LegViewModel
{
public int Length { get; set; }
}
jquery post を使用して、これに JSON を送信します。
<script>
$(function () {
$("#mybutton").click(function () {
$.ajax({
type: "POST",
data: {
Height: 23,
Legs: [
{
Length: 45,
}
]
}
});
});
});
</script>
<button id="mybutton">hello world!</button>
このコントローラーアクションに投稿しています:
[HttpPost]
public ActionResult Save(PersonViewModel model)
{
return Json(new { success = true });
}
リスト内の要素の数Height
と同様に、 a の値PersonViewModel
が入力されますが、リスト内の各要素は入力されません。配列に45の要素が 1 つ含まれると予想される場合、プロパティは 0 のままです。Legs
LegViewModel
Length
Legs
Length
これは、リストをまったく使用しない場合も同じであることに注意してくださいPersonViewModel.Legs property, but still as the
。
// view model
public class PersonViewModel
{
public int Height { get; set; }
//public List<LegViewModel> Legs {get;set;}
public LegViewModel Leg { get; set; }
}
public class LegViewModel
{
public int Length { get; set; }
}
// view
$("#mybutton").click(function () {
$.ajax({
type: "POST",
data: {
Height: 23,
Leg:
{
Length: 45,
}
}
});
})
JSON を使用してネストされたビュー モデルを設定するにはどうすればよいですか? 私が見逃したものはありますか、それとも MVC でこれを行うことはできませんか?