Introduction

There are three ways to pass a value or data from a view to an action.

The data that is passed

A Form Collection is the data that is passed in a form submission when we use the HTTP POST Method.

The following database script is available in the attached file.

  1. create table tbl_FormCollectionUse
  2. (
  3. ID int identity primary key,
  4. Name varchar(50),
  5. Mobile bigint,
  6. EmailID varchar(100),
  7. Address varchar(100)
  8. )
Start Visual Studio 2010 then Start a New MVC 3 (With Razor View Engine).

Add a New Controller to the project named “FormCollection”.

Steps



Steps



FormCollectionController.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcApplication3.Controllers
  7. {
  8. public class FormCollectionController : Controller
  9. {
  10. string _Name, _EmailID, _Address;
  11. long _MobileN0;
  12. //
  13. //
  14. // GET: /FormCollection/
  15. public ActionResult Index()
  16. {
  17. return View();
  18. }
  19. }
  20. }
Add a View to the Index() Action method.



Index.cshtml

  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. <h2>Index</h2>
  5. <h6></h6>
  6. <h6> I will Retrieve data through the Form Collection and Insert it to the database.</h6>
  7. <h6>@Html.ActionLink("New Record", "NewRecord", "FormCollection")</h6>
Add a new Action Method as in the following:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcApplication3.Controllers
  7. {
  8. public class FormCollectionController : Controller
  9. {
  10. string _Name, _EmailID, _Address;
  11. long _MobileN0;
  12. //
  13. //
  14. // GET: /FormCollection/
  15. public ActionResult Index()
  16. {
  17. return View();
  18. }
  19. public ActionResult NewRecord()
  20. {
  21. return View();
  22. }
  23. }
  24. }
Add a View to the Action method as in the following:



Design The View Depending on our requirements

NewRecord.cshtml
  1. @{
  2. ViewBag.Title = "NewRecord";
  3. }
  4. <h2>NewRecord</h2>
  5. <h6>@Html.ActionLink("Go Back", "Index", "FormCollection")</h6>
  6. @using (@Html.BeginForm("Insert", "FormCollection", FormMethod.Post))
  7. {
  8. <table>
  9. <tr>
  10. <td>@Html.Label("Name")</td><td>:</td><td>@Html.TextBox("TxtName")</td>
  11. </tr>
  12. <tr>
  13. <td>@Html.Label("Mobile No")</td><td>:</td><td>@Html.TextBox("Mobile")</td>
  14. </tr>
  15. <tr>
  16. <td>@Html.Label("EmailID")</td><td>:</td><td>@Html.TextBox("TxtEmailID")</td>
  17. </tr>
  18. <tr>
  19. <td>@Html.Label("Address")</td><td>:</td><td>@Html.TextBox("TxtAddress")</td>
  20. </tr>
  21. <tr>
  22. <td>@Html.Label("Save Your Details")</td><td>:</td><td><input type="submit" value="Submit" /></td>
  23. </tr>
  24. <tr>
  25. <td>@Html.Label("Reset Your Details")</td><td>:</td><td><input type="reset" value="Reset" /></td>
  26. </tr>
  27. </table>
  28. }
Explanation To The Design



Then add a new Action Method to the Controller as in the following:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcApplication3.Controllers
  7. {
  8. public class FormCollectionController : Controller
  9. {
  10. string _Name, _EmailID, _Address;
  11. long _MobileN0;
  12. //
  13. //
  14. // GET: /FormCollection/
  15. public ActionResult Index()
  16. {
  17. return View();
  18. }
  19. public ActionResult NewRecord()
  20. {
  21. return View();
  22. }
  23. public ActionResult Insert(FormCollection Fc)
  24. {
  25. return View();
  26. }
  27. }
  28. }
Notice: The new Action Method is declared with a FormCollection Parameter.

Then add a View for the Insert Action as in the following method



Insert.cshtml

  1. @{
  2. ViewBag.Title = "Insert";
  3. }
  4. <h2>Insert</h2>
  5. <h6>@Html.ActionLink("Go Back", "Index", "FormCollection")</h6>
  6. <h6>@ViewBag.Msg</h6>
Create the table if not created and add it to the Entity Framework Model Designer as in the following:
  1. create table tbl_FormCollectionUse
  2. (
  3. ID int identity primary key,
  4. Name varchar(50),
  5. Mobile bigint,
  6. EmailID varchar(100),
  7. Address varchar(100)
  8. )
The following shows how to add a table to the (EntityFramework Designer) Model.edmx file.

At the Model.edmx select Update Model from Database -> Update Wizard then select the Table Name then click Finish.

Modify the code at Insert the Action method and retrieve and save that data to the database.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcApplication3.Controllers
  7. {
  8. public class FormCollectionController : Controller
  9. {
  10. string _Name, _EmailID, _Address;
  11. long _MobileN0;
  12. //
  13. //
  14. // GET: /FormCollection/
  15. public ActionResult Index()
  16. {
  17. return View();
  18. }
  19. public ActionResult NewRecord()
  20. {
  21. return View();
  22. }
  23. public ActionResult Insert(FormCollection Fc)
  24. {
  25. using (MVC_PracticeEntities objContext = new MVC_PracticeEntities())
  26. {
  27. tbl_FormCollectionUse Tbl = new tbl_FormCollectionUse();
  28. //
  29. Tbl.Name = Fc["TxtName"].ToString();
  30. Tbl.Mobile = Convert.ToInt64(Fc["Mobile"]);
  31. Tbl.EmailID = Fc["TxtEmailID"].ToString();
  32. Tbl.Address = Fc["TxtAddress"].ToString();
  33. //
  34. objContext.AddTotbl_FormCollectionUse(Tbl);
  35. //
  36. int i = objContext.SaveChanges();
  37. if (i > 0)
  38. {
  39. ViewBag.Msg = "Data Saved Suuceessfully.";
  40. }
  41. }
  42. //_Name=F
  43. return View();
  44. }
  45. }
  46. }
Run the application and test it.



Click on New Record.



Click on Submit.



Ad some more data and watch the DB Table.



I am providing all the code and the scripts again.

Index.cshtml

  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. <h2>Index</h2>
  5. <h6></h6>
  6. <h6> I will Retrieve data through the Form Collection and Insert it to the database.</h6>
  7. <h6>@Html.ActionLink("New Record", "NewRecord", "FormCollection")</h6>
NewRecord.cshtml
  1. @{
  2. ViewBag.Title = "NewRecord";
  3. }
  4. <h2>NewRecord</h2>
  5. <h6>@Html.ActionLink("Go Back", "Index", "FormCollection")</h6>
  6. @using (@Html.BeginForm("Insert", "FormCollection", FormMethod.Post))
  7. {
  8. <table>
  9. <tr>
  10. <td>@Html.Label("Name")</td><td>:</td><td>@Html.TextBox("TxtName")</td>
  11. </tr>
  12. <tr>
  13. <td>@Html.Label("Mobile No")</td><td>:</td><td>@Html.TextBox("Mobile")</td>
  14. </tr>
  15. <tr>
  16. <td>@Html.Label("EmailID")</td><td>:</td><td>@Html.TextBox("TxtEmailID")</td>
  17. </tr>
  18. <tr>
  19. <td>@Html.Label("Address")</td><td>:</td><td>@Html.TextBox("TxtAddress")</td>
  20. </tr>
  21. <tr>
  22. <td>@Html.Label("Save Your Details")</td><td>:</td><td><input type="submit" value="Submit" /></td>
  23. </tr>
  24. <tr>
  25. <td>@Html.Label("Reset Your Details")</td><td>:</td><td><input type="reset" value="Reset" /></td>
  26. </tr>
  27. </table>
  28. }
Insert.cshtml
  1. @{
  2. ViewBag.Title = "Insert";
  3. }
  4. <h2>Insert</h2>
  5. <h6>@Html.ActionLink("Go Back", "Index", "FormCollection")</h6>
  6. <h6>@ViewBag.Msg</h6>
FormCollectionController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcApplication3.Controllers
  7. {
  8. public class FormCollectionController : Controller
  9. {
  10. string _Name, _EmailID, _Address;
  11. long _MobileN0;
  12. //
  13. //
  14. // GET: /FormCollection/
  15. public ActionResult Index()
  16. {
  17. return View();
  18. }
  19. public ActionResult NewRecord()
  20. {
  21. return View();
  22. }
  23. public ActionResult Insert(FormCollection Fc)
  24. {
  25. using (MVC_PracticeEntities objContext = new MVC_PracticeEntities())
  26. {
  27. tbl_FormCollectionUse Tbl = new tbl_FormCollectionUse();
  28. //
  29. Tbl.Name = Fc["TxtName"].ToString();
  30. Tbl.Mobile = Convert.ToInt64(Fc["Mobile"]);
  31. Tbl.EmailID = Fc["TxtEmailID"].ToString();
  32. Tbl.Address = Fc["TxtAddress"].ToString();
  33. //
  34. objContext.AddTotbl_FormCollectionUse(Tbl);
  35. //
  36. int i = objContext.SaveChanges();
  37. if (i > 0)
  38. {
  39. ViewBag.Msg = "Data Saved Suuceessfully.";
  40. }
  41. }
  42. //_Name=F
  43. return View();
  44. }
  45. }
  46. }