这是我的模型:
public class Items { public string Foo { get; set; } public string Bar { get; set; } }
控制器:
public ActionResult Index() { var model = new List<Items> { new Items { Foo = "foo",Bar = "bar" },new Items { Foo = "ai",Bar = "ia" },new Items { Foo = "one",Bar = "two" } }; return View(model); } [HttpPost] public ActionResult Index(List<Items> model) { return View(model); }
查看(索引):
@using (Html.BeginForm()) { for (int i = 0; i < Model.Count; i++) { <div onclick="$(this).remove();"> @Html.TextBoxFor(model => model[i].Foo) <br/> @Html.TextBoxFor(model => model[i].Bar) </div> } <div> <input type="submit"/> </div> }
我删除了第二对:
<div onclick="$(this).remove();"> <input name="[0].Foo" type="text" value="foo"> <br> <input name="[0].Bar" type="text" value="bar"> </div> <div onclick="$(this).remove();"> <input name="[2].Foo" type="text" value="one"> <br> <input name="[2].Bar" type="text" value="two"> </div>
发布时,我只获得第一对(“foo”和“bar”).这是因为第三对的索引为“2”.我想得到两个对(不使用FormCollection.我希望它自动绑定).实际上,我在表单上有很多其他输入,所以我不想重新加载并重新附加索引到每个输入.你能帮助我吗?
解决方法
这可能对你有所帮助….
需要在每个项目上放置隐藏字段…
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。