For the most part binding to a collection works exactly the same way. You still need a key and the names of the textboxes still be in a specific format for the model binder to work.
However, with MVC4 and the new SPA (single page application) template you can use upshot.js to automatically synchronize changes to the server. The way it works is that upshot has a datasource that is watching your collection, you can set it up to automatically
push changes to the server, but I believe they are pushed as individual changes this way. You can also configure upshot to push all the changes at once, what's cool is that you don't really have to do much server side other than set up a special kind of controller
that exposes your data context, upshot/datacontroller does the rest for you.
I need to be able to add records as well, which I can't seem to do outside the for loop (loses the index).
Can you not add them from JavaScript?
In any event, yes, if you want to add more "rows" than are in the array then you will have to manually construct the id and name fiels with the correct array index. Just look at the rendered HTML for the page and you'll see the pattern.
JohnLocke
Contributor
3710 Points
792 Posts
Editing a variable length list for mvc3?
Apr 11, 2012 03:18 PM|LINK
I've used the solution that Steve Sanderson posted over 2 years ago
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
I was wondering if mvc3 (or 4) has provided a way of doing this without using a custom html helper (BeginCollectionItem)
CodeHobo
All-Star
18669 Points
2648 Posts
Re: Editing a variable length list for mvc3?
Apr 11, 2012 03:36 PM|LINK
For the most part binding to a collection works exactly the same way. You still need a key and the names of the textboxes still be in a specific format for the model binder to work.
However, with MVC4 and the new SPA (single page application) template you can use upshot.js to automatically synchronize changes to the server. The way it works is that upshot has a datasource that is watching your collection, you can set it up to automatically push changes to the server, but I believe they are pushed as individual changes this way. You can also configure upshot to push all the changes at once, what's cool is that you don't really have to do much server side other than set up a special kind of controller that exposes your data context, upshot/datacontroller does the rest for you.
Blog | Twitter : @Hattan
BrockAllen
All-Star
28072 Points
4996 Posts
MVP
Re: Editing a variable length list for mvc3?
Apr 11, 2012 03:40 PM|LINK
This is possible in MVC2 and you don't have to build a custom html helper.
<table> <tbody> @{ var list = Model.Products.ToArray(); for(int i = 0; i < list.Length; i++) { <tr> <td>Name: @Html.TextBoxFor(model => list[i].ProductName)</td> <td>Price: @Html.TextBoxFor(model => list[i].UnitPrice)</td> </tr> } } </tbody> </table>Then your server code can accept an IEnumerable<YourModel>.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
JohnLocke
Contributor
3710 Points
792 Posts
Re: Editing a variable length list for mvc3?
Apr 11, 2012 03:48 PM|LINK
I'll look into it Hobo.
I need to be able to add records as well, which I can't seem to do outside the for loop (loses the index).
BrockAllen
All-Star
28072 Points
4996 Posts
MVP
Re: Editing a variable length list for mvc3?
Apr 11, 2012 03:53 PM|LINK
Can you not add them from JavaScript?
In any event, yes, if you want to add more "rows" than are in the array then you will have to manually construct the id and name fiels with the correct array index. Just look at the rendered HTML for the page and you'll see the pattern.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/