handle form submit for multiple items
I have a function in my MVC3 application that presents a user with a set of form inputs. There are only a few fields and the user will likely be entering many items at a time (business expenses). To speed up the entry we allow the user to keep adding rows to the entry page so the user can enter many expenses at a time without having to save or submit, then they can review them and submit them all at once.
My question... I have two thoughts on handling the submission of multiple ho开发者_开发百科mogeneous items. Which one is better?
One large form on the client, Javascript to separate and serialize the data for each item OnSubmit. A server side method to iterate through the request to create multiple items.
an Html Form tag for each item. Javascript to handle OnSubmit to make an individual ajax call to the server for each item. JavaScript callback functions to show the user when each one is saved and a server side method that handles one item at a time?
Thoughts?
I haven't used it myself, but an MVC action should be able to receive a List of items just like it receives a single one, as long as they are properly generated.
After a quick search, I found a few guides on how to do it, this seems the simplest: http://blog.donnfelker.com/2010/02/27/editable-grid-list-binding-in-mvc2/
Take into account that MVC just generates standard HTML. That means that, even though in the example the author is creating the fields in the View, you could also create the fields with Javascript, as long as you use the same naming convention that MVC expects.
So, to answer your question, I would go for a single form :)
精彩评论