ASP.NET MVC FormCollection Example


1. FormCollection class contains the form value providers for the application.

2. FormCollection class is used to receive form’s post values in the controller.
3. Controller receives the form values in key value format in FormCollection class’s object.
4. Keys in FormCollection can be accessed using key names or index.

Watch Video


Following is the way we use FormCollection :


Step 1 :

First we will create a model class which will have properties to be used on form. Model class named Register is shown below.




Step 2 :

Once our Register model class is ready. We will move to prepare controller and view. We will create a view and inside that view a form, holding properties we defined in model.
Our View and Form will look like as shown below.




Step 3 :

On submit of this Form we will post Form Data to SubmitForm Action method in controller, where we are going to use FormCollection to access the form's values.
We can access form's Data from FormCollection by using index of the key or by specifying the name of the key, which is name attribute of that particular control.


There are two ways to access value from FormCollection key :

Way 1 :

In this method we will use key index to fetch data from the FormCollection. We can use it as shown below.







In the above screen short we are receiving FormCollection class's object as a parameter, which contains form values. Further we are using key index to fetch the data.


Way 2 :

In this method we will use key names to fetch data from the FormCollection. We can use it as shown below.



In above screenshot we are using key names to access values from FormCollection. These key names are the name attribute of the properties we used on our view.



Points To Remember :

1. FormCollection is use to access Form's post Data on the controller.
2. FormCollection class contains the form value providers for the application.








4 comments:

  1. u must tell that when to create strongly type view and when not .As this cause confusion.

    ReplyDelete
  2. Ok. Thanks for the suggestion.

    ReplyDelete
  3. Very useful thanks for sharing (c)

    ReplyDelete
  4. Very Good Article............Clearly Explained...........

    ReplyDelete