2

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.

Robaticus
  • 22,857
  • 5
  • 54
  • 63
Ryan O'Neill
  • 1,710
  • 2
  • 13
  • 24

1 Answers1

4

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>?

Community
  • 1
  • 1
John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • Thanks, I was able to use that link to get the functionality I wanted. The piece I was missing was the EditorFor Html helper. – Ryan O'Neill Feb 16 '11 at 16:25