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.
- <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVC_Test.Models.person>" %>
- <!DOCTYPE html>
- <html>
- <head runat="server">
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <% using(Html.BeginForm("savedata","person")){ %>
- Name <%= Html.TextBox("name") %> <br />
- Surname : <%= Html.TextBox("surname") %> <br />
- Age: <%= Html.TextBox("age") %> <br />
- Gender <%= Html.RadioButton("gender","Male") %> Male
- <%= Html.RadioButton("gender","Female") %> Female <br />
- <input type="submit" name="Submit" value="Submit" />
- <%} %>
- </div>
- </body>
- </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.

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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MVC_Test.Models;
- namespace MVC_Test.Controllers
- {
- public class personController : Controller
- {
- public ActionResult Index()
- {
- return View("Index");
- }
- public void savedata(FormCollection fomr)
- {
- string name = Request.Form["name"];
- string surname = Request.Form["surname"];
- string age = Request.Form["age"];
- string gender = Request.Form["gender"];
- }
- }
- }
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.

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.

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.

Pergin SheniPosted Aug 12, 2019, 12:56 AM
Well explained. You saved my day. Thank you for valuable note!
Muhammad khanPosted Apr 2, 2019, 1:57 AM
I have scenario like Marks entry in MVC Approximately 3000 student's marks entry at a timeI can't show 3000 text boxes on single page so please can you help anyone who already worked on this scenario (Suggest me BEST approach)
Pratham DavePosted Jan 6, 2019, 11:49 AM
How to create dynamic only form using asp.net mvc and jquery?
Priya SwaminathanPosted Oct 14, 2017, 12:14 AM
Thanks its very useful
Sherlock Vilmes Vic VilmesPosted Jul 24, 2017, 1:14 PM
If I created my form view using data-binding technique such as `@model RandomNameSpace.Person`, then in the POST action, is it necessary to pass in `FormCollection f` or is it better to just pass in `Person p` ?
Rukshar KhanPosted Jul 4, 2017, 11:43 PM
Good example on form collection
SubashPosted Oct 19, 2016, 5:37 AM
Form collection is misspelled in first paragraph pls change if possible
SubashPosted Oct 19, 2016, 5:36 AM
Thank you sourav is it possible to set the form collection request.form.allkeys in other data as other controller or view?
Rizwan AshrafPosted Jun 1, 2015, 9:06 AM
Me too
Suchet BoonyasaiPosted Apr 8, 2015, 10:37 PM
Want to know too
Faruk ShaikhPosted Feb 12, 2015, 3:30 AM
hi sourav when you retrieving form values via Request.Form object so why did you have used the Form collection object as parameter ?