views:

45

answers:

1

Edit: I am trying to bind a single view model object that contains a List to a form so that the post maps back to the same view model object.

Is there any code out there that could effectively do this in MVC 2?

EditorFor( m => m, "Students", "Students[n]") // wrong but sorta close
// the third parameter specifies the ID of the html control
// ideally, the "Students[n]" could be a prefix for the inner fields

where the html output would be:

<input id="Students[0]_Name" type="text" value="" />
<input id="Students[1]_Name" type="text" value="" />
<input id="Students[2]_Name" type="text" value="" />

and the id assembly could be within the partial "class" view (UI Template)?

...

Now that I think about it, I wonder if this would prefix all the fields with the name:

<% foreach( Student student in Model) { %><%
       EditorFor( m => student, "Student", 
                  "Students[" + Model.IndexOf(student) + "]" ) %><%
   } %>

Odd... That puts that third parameter string (eg, "StuffHere") between Students and Name:

id="Students_StuffHere_Name" name="Students.StuffHere.Name"
+1  A: 

Didn't understand your question very well, but with the RC2 of asp.net mvc you can now write code like Html.EditorFor(m=>m.Students[i]) as explained here. Regards.

uvita
You nailed the answer. Where can I get examples of how to use collections with that syntax? I can see they use Arrays, but can they use Lists? Easy enough to switch, but I am also concerned with binding back when they post. The whole point is to pass a single object as a parameter to the Action Method (or collection of objects within a viewModel object).
Dr. Zim
You can have a look at couple of links like http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx and http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx also google for some more info, but you shouldn't have any problem in re hydrating your model in a post. Regards
uvita