【发布时间】:2014-02-20 03:03:30
【问题描述】:
我有一个从未遇到过的情况,我的模型中有一个集合,但我一次只显示一个项目。这是一个简单的例子......
型号:
public class MyViewModel
{
//some other fields
public IList<SomeObject> MyCollection { get; set; }
}
查看:
@model MyViewModel
<ul>
for(int i; i < Model.MyCollection.Count(); i++)
{
//user can select an item from list and for the chosen item,
//show fields to edit below
<li>@Model.MyCollection[i].Name;
}
</ul>
@Html.LabelFor(m => m.MyCollection[TempData["Index"]].SomeProperty)
@Html.TextBoxFor(m => m.MyCollection[TempData["Index"]].SomeProperty)
//other fields
这包含在局部视图中的表单中,当用户从名称列表中选择一个项目并且他们的选择反映在TempData["Index"] 中时,该表单会刷新。
我的问题是,当我将其发布到需要MyViewModel model 的控制器时,内部MyCollection 的集合将仅显示一项(以显示的为准)。要捕获整个集合,我必须遍历整个集合并为每个不是当前选定对象的对象使用隐藏字段(这是一个相当复杂的模型)还是有更好的方法我应该这样做?
【问题讨论】:
标签: asp.net-mvc-4 razor