Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit a table with dynamic rows of data via asp.net mvc or jquery?

i'm totally noob to asp.net mvc and currently i'm writing an web app which allows user to select multiple product from a listbox and add them to a dynamic table with rows that can be added or removed.

thus i'm thinking of using jquery to add and remove the table row, thus the table row will be added via append("xxx").

but the thing is i'm not that familiar with asp.net mvc form submission, from my understanding thus far, i can retrieve the value later via form["id"] but if i were to create the table row via that method, i'm thinking it's either going to be with the same id, or i can use a running no id and a hidden input to keep track of how many products that is added.

also i probably want to hide the whole product table when all the products were removed.

p.s. kinda feel that my way is sort like a big fugly messy hack which will come back n bite me one day.

Questions:

  1. does asp.net mvc has a good way to handle all of this? i've read something about mvc tempdata.
  2. and is there a better way for me to handle the dynamic product generation? to make things easier later for form submission in asp.net mvc
  3. and also i found that you can actually submit the form via jquery too, is there any pros n cons between the two method? in terms of maybe performance, security, maintenance, etc?

thanks

like image 357
melaos Avatar asked Mar 31 '10 09:03

melaos


People also ask

How can add row in Table with button click in asp net?

You can add row just by using: TableRow row1=new TableRow(); TableRows. add(row1);


1 Answers

You can post collections of objects; in MVC. Here is an example: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Basically, you can use JQuery to create the table, but ensure the ID's of the controls reference this scheme, so for the next row to add using JQuery, ensure the controls to input the data use the next index in the list, along with the same name as the property in the underlying bound object.

When you post to the server, you can loop through this collection.

To answer the others: you can submit form via JQuery if you don't want a postback to the server (can use ASP.NET MVC to do too)... otherwise, you don't need to worry about the JQuery submit. So that may add usability.

HTH.

like image 121
Brian Mains Avatar answered Oct 27 '22 00:10

Brian Mains