Click here to Skip to main content
14,766,117 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Now i am really confused about formcollection in MVC3. I add HTML table ,named "idxtbl", in my view page. Then i populated with 5 rows. How should i get all the rows when page is post back ?

Private ActionResult GetRow(Formcollection frmcol){
          //How should i get all the rows in HTML table from GetRow View Page.

Karthik. A 9-Jul-12 21:52pm
Only a form field (like textbox, textare etc) will be posed back using a formcollection. And MVC supports automatic model binding.

// txtName is a text box in the form
public ActionResult Index(string txtName)
return View();

A table is just html and that WON'T be posted back in a formcollection.
Karthik. A 9-Jul-12 21:55pm
What is your requirement exactly? I cannot think of a reason you would want to do this. If you could elaborate, one of us could help!
dartfrog 10-Jul-12 0:15am
Hi Karthik, Thanks for your reply. Actually, there are two text boxes and Two buttons in view page. user need to key in two text boxes first and then press the button so all the records that user key in will be added inside HTML table(Using JQuery). When user populated with rows in HTML table, need to press another button(will trigger to GetRow action) to save all the records within the database.
Karthik. A 10-Jul-12 10:59am
Can't you save the data user enters in to the database already using $.ajax and then just append this row to the table? Because in order to send an entire table, you may have to maintain a whole lot of hidden fields. I would do something like this:

1. Have a url /home/add/ which gets 2 values
2. Call it using jQuery's $.ajax passing the value of these 2 text boxes
3. The "Add" method would return json indicating the result of add
4. Then use jquery again to add it to the table.
5. By this way you eliminate the 2nd button altogether.
6. Then when you are re-displaying the page, you pass the values in the db as the model

Hope this helps!
dartfrog 10-Jul-12 22:31pm
Ok Thanks, currently i tried with one hidden textbox. i store all the values in this hidden textbox such as 'Val1-Val2|Val3-Val4|Val5-Val6'
I'll try with your way also.

Thanks for your reply.

1 solution

You might try binding a collection to the data in your table, but I suspect strongly that you'll be iterating over the form data to find and parse it yourself. There are some frameworks for MVC3 and tables, perhaps they will help ( no, I've not used one )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900