我是一位經驗豐富的.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; }
}
和我的控制器有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)
}
我只能添加字段爲基本類型性質在,但不適合的對象。
我真的很感激任何幫助!
請參閱答案[這裏](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796)和[這裏](http://stackoverflow.com /問題/ 28019793 /提交 - 同局部視圖所謂-多次數據到控制器/ 28081308#28081308)。請注意,您不能將'BeginCollectionItem'用於嵌套集合 –
並且有關動態創建嵌套集合的工作示例,請參閱[此DotNetFiddle](https://dotnetfiddle.net/wqE6Rb) –