所以我有以下代码:
@model Project.Models.ViewModels.SomeViewModel
@using (Html.BeginForm("SomeAction", "SomeController", new { id = Model.Id}))
{
for(int i = 0; i < Model.SomeCollection.Count(); i++)
{
@Html.HiddenFor(x => Model.SomeCollection.ElementAt(i).Id)
<div class="grid_6">
@Html.TextAreaFor(x => Model.SomeCollection.ElementAt(i).Text, new { @style = "height:150px", @class = "grid_6 input" })
</div>
}
<div class="grid_6 alpha omega">
<input type="submit" value="Next" class="grid_6 alpha omega button drop_4 gravity_5" />
</div>
}
在控制器端,我有以下内容:
[HttpPost]
public ActionResult SomeAction(int id, SomeViewModel model)
{
return PartialView("_SomeOtherView", new SomeOtherViewModel(id));
}
我的视图模型是这样设置的:
public class SomeViewModel
{
public SomeViewModel()
{
}
public IEnumerable<ItemViewModel> SomeCollection { get; set; }
}
public class ItemViewModel{
public ItemViewModel(){}
public int Id {get;set;}
public string Text{get;set;}
}
如果执行 SomeAction,SomeCollection 始终为空。为了显示用户更新的值,我必须做什么。文本属性和 ID 字段。