In this Video, I am going to discuss FormCollection in ASP.NET MVC Application. Please read our previous Video where we discussed Business Objects as Models in the ASP.NET MVC Application. We are also going to work with the same example that we worked on in our previous Video. Here, in this Video, we will create a view to insert a new employee into the database table Employee using the FormCollection class that is available in ASP.NET MVC Framework.
What is FormCollection in ASP.NET MVC Application?. The Complete ASP.NET MVC Developer Course 2023 [Videos].
Creating a view to inserting data using FormCollection in MVC
We want to create a view as shown below to create a new employee.
Creating the Create Action Method:
Please Copy and paste the following “Create” action method in the EmployeeController class. Please note the following action method is decorated with the HttpGet attribute. This makes the Create Action Method to respond only to the “GET” request.
Adding the Create View:
Now lets add the “Create” view. To do this right-click on the “Create” action method and select “Add View” from the context menu. Set
View name = “Create”
Template = “Create”
Model class = Employee (BusinessLayer)
Click on the “Add” button as shown below.
At this point “Create.cshtml” view will be added to the “Employee” folder. Run the application and navigate to the URL “http://localhost:54094/Employee/Index“. Click on the “Create New” link. It will navigate to the URL http://localhost:54094/Employee/Create URL and will display the following page.
A form with text boxes to add a new employee is rendered. For employee “Gender“, it is ideal to have a dropdown list instead of a text box. To achieve this replace the following line of code.
After the changes, the complete code for the create view as shown below
Now, run the application and notice that a dropdown list is rendered for “Gender”. Now, if you click on the “Create” button you will get an error message stating – The resource cannot be found. This is because we dont have the “Create” action method that can handle HTTPPost requests.
What is the FormCollection Class in ASP.NET MVC?
The FormCollection class in ASP.NET MVC will automatically receive the posted form values in the controller action method in the form of key/value pairs. The values can be accessed using either key names or indexes. We can use the FormCollection to loop through each key and its value that is posted to the server. Lets add the following Create Post method in the employee Controller class.
Now run the application and fill the view and click on the create button as shown below.
The output is as shown below.
Lets create a stored procedure to insert the employee object in the Employee table
Add the following method to the EmployeeBusinessLayer.cs file.
To save form data to a database table modify the create (HttpPost) action method EmployeeController.cs file as shown below.
Now Run the application and see everything is working as expected.
But the question is do we really have to write all the dirty codes of retrieving data from FormCollection class and assign it to the properties of the “employee” object. The answer is no. This is the job of the model binder in the ASP.NET MVC Application.