在一个 View 中,我有一组映射到 List<int>
的复选框。 。
查看:
<input name="foo.IntegerList" type="checkbox" value="1">
<input name="foo.IntegerList" type="checkbox" value="2">
<input name="foo.IntegerList" type="checkbox" value="3">
<input name="foo.IntegerList" type="checkbox" value="4">
<input name="foo.IntegerList" type="checkbox" value="5">
行动:
[HttpPost]
<public ActionResult DoStuff(Foo foo)
//do stuff
型号:
public class Foo
{
IEnumerable<int> IntegerList
}
将此表单帖子提交到 DoStuff 操作会为其填充 List<int>
。
现在,我必须使用IntegerList
在另一个 View 中,并作为隐藏字段。在另一个 View 中,我使用以下代码:
@Html.HiddenFor(p => p.IntegerList)
但这会被翻译为以下 HTML:
<input name="IntegerList" type="hidden" value="">
请注意,该值尚未设置。我该如何制作Html.HiddenFor
生成正确的隐藏字段?
最佳答案
您必须迭代它们并将名称与索引放在一起:
@for(int i = 0; i < Model.IntegerList.Count; i++)
<input name="IntegerList[@i]" type="hidden" value="@Model.IntegerList[i]">
}
使用 HiddenFor
就可以这样完成
@for(int i = 0; i < Model.IntegerList.Count; i++)
@Html.HiddenFor(model => Model.IntegerList[i])
}
关于asp.net - 将复选框组存储在@Html.HiddenFor中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39405617/