在viewModel中编辑嵌套列表

时间:2013-03-26 09:12:20

标签: c# asp.net-mvc-3 viewmodel

我正在开发一个MVC3项目,目前我正在浏览我的viewmodel中的列表。我希望能够在不进行主视图发布(后退)的情况下将项目添加到列表中。当我按下提交按钮时,我希望viewmodel包含列表中的项目。

实现此功能的最佳方法是什么?

提前致谢,

1 个答案:

答案 0 :(得分:1)

此解决方案最初是为MVC2编写的,但在MVC 3和4中有效。

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

<强>更新

如果您不想使用@ Html.BeginCollectionItem,则不必使用。使用以下内容同样有效:

@for (int i = 0; i < Model.Items.Count; i++)
{
    @* 
        You can replace this line with whatever you require but
        for simplicity I prefer to create an editor template for the sub type
    *@
    @Html.EditorFor(m => m.Items[i])      
}