如何模型绑定到 List?
我正在努力完成这样的事情。我觉得这是可能的,如果不是,可能是 MVC 框架中的疏忽?
视图:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<List<MyViewModel>>" %>
...
<% foreach (MyViewModel vm in Model) {
Html.RenderPartial("MyViewModelPartial", vm);
} %>
部分视图是可编辑的表单,强类型化为单个 MyViewModel,并使用 MyViewModel 类上的 DataAnnotations 来验证
控制器:
public ActionResult FooController(List<MyViewModel> vml)
{
...
}
这可能吗?这似乎是在 MVC 中构建网格/表结构的最合乎逻辑的方法(每个部分视图都是一个表行),但我似乎无法让它工作,我最终在控制器中使用 FormCollection 来循环整个过程当形式,而且很乱。
I'm trying to accomplish something like this. I feel like it's possible, and if not probably an oversight in the MVC framework?
View:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<List<MyViewModel>>" %>
...
<% foreach (MyViewModel vm in Model) {
Html.RenderPartial("MyViewModelPartial", vm);
} %>
The partial view being an editable form, strongly typed to a single MyViewModel, and use the DataAnnotations on the MyViewModel class to validate
Controller:
public ActionResult FooController(List<MyViewModel> vml)
{
...
}
Is this possible? This seems like the most logical way to build grid/table structures in MVC(with each partial view being a table row) but I can't seem to get it to work and I end up using FormCollection in my controller to loop through the whole dang form, and it's just messy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅:
http://haacked.com/ archive/2008/10/23/model-binding-to-a-list.aspx
链接到:
复杂模型绑定到列表
ASP.NET MVC 如何:如何绑定 List类型的属性?
See:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Which is linked to from:
Complex model binding to a list
How ASP.NET MVC: How can I bind a property of type List<T>?