Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC3: Can you post and bind an array of objects?

I have been tasked with the job of creating a form that allows a user to add one or more groups of answers to it. For example, they will make a selection from a drop down which will then add another set of inputs to the form. They can repeat this process X number of times.

What's the best way to handle this in terms of processing on the server?

I understand I could probably bind each element to a list, and then loop through each list knowing each value in each list was 'linked' by index.

But is there a better way? Hope this makes sense. I don't have example code as of yet.

like image 434
Sergio Avatar asked Jul 06 '11 08:07

Sergio


1 Answers

Take a look at Phil Haack's great article about model binding to a list in mvc. If your dynamically added..

another set of inputs

..is returned from ajax call to controller, you can use this in conjunction with HtmlFieldPrefix Property and generate desired set of inputs with indexed name. Then, MVC will automatically bind your values as explained in post.

like image 101
archil Avatar answered Sep 28 '22 03:09

archil