5

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.

Andrew
  • 9,967
  • 10
  • 64
  • 103
Sergio
  • 9,761
  • 16
  • 60
  • 88
  • Are you trying to make this work without posting to the server for each response? or are you open to that approach. I think to start the simplest, most direct approach will be the best. Also, I can show this where the data is posted w JS. – Glenn Ferrie Jul 06 '11 at 08:46
  • Im not adverse to posting to the server, but my UI in general is quite complex due to the fact Im using jQuery Mobile. I'm still trying to get my head around it all tbh – Sergio Jul 07 '11 at 08:25

2 Answers2

3

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.

JoeBrockhaus
  • 2,745
  • 2
  • 40
  • 64
archil
  • 39,013
  • 7
  • 65
  • 82
  • This answer is a bit more clear about how to use `TemplateInfo.HtmlFieldPrefix` :: http://stackoverflow.com/a/6617869/237723 – JoeBrockhaus Jan 28 '15 at 14:34
2

have a look at following articles they are worth your consideration http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
http://zahidadeel.blogspot.com/2011/05/master-detail-form-in-aspnet-mvc-3-ii.html
http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/
if you are into knockout js you can also consider
http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • Lots of good info there. Might take me a while to digest. The solutions are not what I'd call totally straightforward, but then I guess 'dynamic' forms aren't straightforward by their nature. – Sergio Jul 07 '11 at 08:24