Retrieving Data Using Form Collection and Inserting Into ASP.Net MVC 3

Introduction

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

  • Parameterized Query.
  • Form Data.
  • Model Binding.

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.

  • What we will do in this application.
  • We will learn retrieving the data by using Form Collection.
  • For all this operation we will use the EntityFramework.

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.   
  7. namespace MvcApplication3.Controllers  
  8. {  
  9.     public class FormCollectionController : Controller  
  10.     {  
  11.         string _Name, _EmailID, _Address;  
  12.         long _MobileN0;  
  13.         //  
  14.         //  
  15.         // GET: /FormCollection/  
  16.   
  17.         public ActionResult Index()  
  18.         {  
  19.             return View();  
  20.         }  
  21.   
  22.     }  
  23. }  
Add a View to the Index() Action method.



Index.cshtml

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