私は経験豊富な.NET C#ソフトウェア開発者ですが、ほんの数ヶ月前にMVC Razor(MVC 5)で作業を開始しました。MVC Razorは、提出するオブジェクトのリストを作成します。
私は順番にものリストを持っている他のモデルのリストを持っているモデルを持っている私は(検索の時間後)の純
上への答えを見つけることができませんでした小さな状況があります次のようなモデルです。
public class Parent
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public List<GrandChild> GrandChildren { get; set; }
}
public class GrandChild
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
と私のコントローラは、1つの取得のメインである、2つの方法があり、他の一つはしかし、フォームでの私の見解では、私は方法を見つけることができない新しいデータ
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult PostParent(Parent parent)
{
if (ModelState.IsValid)
{
//Do an insert to the DB
return View("~/Views/Home/Index.cshtml");
}
return Json("Error");
}
を投稿するためにポストであります(子供の包み)追加ボタンを作成し、子供や孫のリストに新しいデータを挿入する
@using (Html.BeginForm("PostParent", "Home", FormMethod.Post, new {@class = "form-horizontal", role = "form"}))
{
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
}
私は唯一のプリミティブ型properitesのためではなく、オブジェクトのフィールドを追加することができます。
本当にありがとうございました!
答え[ここ](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796)と[ここ](http://stackoverflow.com)を参照してください。/questions/28019793/submit-same-partial-view-multiple-times-data-to-controller/28081308#28081308)。ネストされたコレクションに 'BeginCollectionItem'を使用できないことに注意してください。 –
ネストされたコレクションを動的に作成する実際の例については、[こちらのDotNetFiddle](https://dotnetfiddle.net/wqE6Rb) –