Understand FormCollection In MVC Controller

This article shows the use of a FormCollection in the MVC architecture. Using the FomrCollection class we can capture the form's values within the controller.

We know that there are various ways to collect data from a view to a collection, FomrCollection is one of them. We will implement a simple example of a FomrCollection shortly.

To implement it we need to create a MVC application.

Here is the view to create the HTML form element

We are using a .aspx view in this application and a HTML helper class to generate HTML elements. If we look closely at the form element we will see that the form data will be submited for the “savedata” action defined within the “person” controller. Have a look at the following code.

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVC_Test.Models.person>" %>  
  2. <!DOCTYPE html>  
  3. <html>  
  4. <head runat="server">  
  5.     <meta name="viewport" content="width=device-width" />  
  6.     <title>Index</title>  
  7. </head>  
  8. <body>  
  9.     <div>  
  10.        <% using(Html.BeginForm("savedata","person")){ %>  
  11.         Name <%= Html.TextBox("name") %> <br />  
  12.         Surname : <%= Html.TextBox("surname") %> <br />  
  13.         Age: <%= Html.TextBox("age") %> <br />  
  14.         Gender <%= Html.RadioButton("gender","Male") %> Male  
  15.         <%= Html.RadioButton("gender","Female") %> Female <br />  
  16.         <input type="submit" name="Submit" value="Submit" />  
  17.        <%} %>        
  18.     </div>  
  19. </body>  
  20. </html>  

When we run the application, we will get the following output. And when the Submit button is pressed, the form data will be submitted to the targeted action.

view to create HTML form element
We now need to create the controller that will return the view and handle the action. In this example we created a “person” controller with two actions. Once we run the person controller it will return an index view that is nothing but the HTML form that we designed.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MVC_Test.Models;  
  7.    
  8. namespace MVC_Test.Controllers  
  9. {  
  10.     public class personController : Controller  
  11.     {  
  12.         public ActionResult Index()  
  13.         {  
  14.             return View("Index");  
  15.         }  
  16.    
  17.         public void savedata(FormCollection fomr)  
  18.         {  
  19.             string name = Request.Form["name"];  
  20.             string surname = Request.Form["surname"];  
  21.             string age = Request.Form["age"];  
  22.             string gender = Request.Form["gender"];  
  23.         }  
  24.     }  
  25. }  

The savedata() action is taking an object of the FormCollection class. When we submit the form then all the control's information will be in the “form” object. Have a look at the following output.

FormCollection class

We are seeing that the controls collection has been created within the “AllKeys” property. Now, if we want to access those form values then we need to use the form element by supplying the name in the object. Here we are getting all the form values.
 
object

Conclusion

In this example we learned to collect all the form elements using the FormCollection class. This is one way to pass a value from a view to the controller. I hope you understood it. Happy learning.


Similar Articles