单击按钮时向表中添加/删除行的最佳方法是什么?我需要从ChildClass属性创建的行(子类是主类/模型中的列表).
目前有一个View(模型是MyMain),它使用RenderPartial引用一个局部视图.
局部视图显示模型的属性,一个名为MyChild的类,它是MyMain中的对象列表.
我想添加和删除按钮来动态添加部分视图中保存的行.
因此,重复添加MyChild以获取列表中的更多行.这可能吗?或者我不应该为此使用部分视图?
更新的代码
下面是我正在使用的当前类和视图,我一直在尝试实现BeginCollectionItem帮助器,但我得到null ref,我试图加载局部视图,尽管if语句说创建一个新实例子类如果不存在 - 为什么会被忽略?
主视图
@using (Html.BeginForm())
{
<table>
<tr>
<th>MyMain First</th>
<th>MyChild First</th>
</tr>
<tr>
<td>
@Html.EditorFor(m => m.First)
</td>
<td>
@if (Model.child != null)
{
for (int i = 0; i < Model.child.Count; i++)
{
Html.RenderPartial("MyChildView");
}
}
else
{
Html.RenderPartial("MyChildView", new MvcTest.Models.MyChild());
}
</td>
</tr>
@Html.ActionLink("Add another", "Add", null, new { id = "addItem" })
</table>
}
Run Code Online (Sandbox Code Playgroud)
局部视图
@model MvcTest.Models.MyChild
@using (Html.BeginCollectionItem("myChildren"))
{
Html.EditorFor(m => m.Second);
}
Run Code Online (Sandbox Code Playgroud)
楷模
public class MyMain …Run Code Online (Sandbox Code Playgroud)