I am going to discuss creating a View to insert a new employee into the database table Employee using FormCollection class, which is available in ASP.NET MVC.
Step 1
Step 2

Step 3

Step 4

You will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name EmployeeModel (this name is not mandatory, you can give any name) and click "Add".

After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".

After clicking on "Next", a window will appear. Choose New Connection. Another window will appear. Add your server name - if it is local, then enter a dot (.). Choose your database and click "OK".

The connection will be added. If you wish, save the connection name as you want. You can change the name of your connection below. It will save the connection in the web config. Now, click "Next",

After clicking on NEXT, another window will appear. Choose the database table name as shown in the below screenshot and click "Finish".

Entity Framework gets added and the respective class gets generated under the Models folder.

Step 5

A window will appear. Choose MVC5 Controller-Empty and click "Add".

After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.

The FormCollection class will automatically receive the posted form values in the controller action method in key/value pairs. Keys & values can be accessed using key names or index.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create(FormCollection formCollection)
- {
- if (ModelState.IsValid)
- {
- foreach (string key in formCollection.AllKeys)
- {
- Response.Write("Key=" + key + " ");
- Response.Write("Value=" + formCollection[key]);
- Response.Write("<br/>");
- }
- }
- return View();
- }

- using MvcFormCollection_Demo.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcFormCollection_Demo.Controllers
- {
- public class HomeController : Controller
- {
- private readonly EmployeeContext _dbContext = new EmployeeContext();
- public ActionResult Index()
- {
- var employee = _dbContext.Employees.ToList();
- return View(employee);
- }
- public ActionResult Create()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create(FormCollection formCollection)
- {
- if (ModelState.IsValid)
- {
- Employee employee = new Employee();
- employee.Name = formCollection["Name"];
- employee.Gender = formCollection["Gender"];
- employee.Age =Convert.ToInt32(formCollection["Age"]);
- employee.Position = formCollection["Position"];
- employee.Office = formCollection["Office"];
- employee.HireDate = Convert.ToDateTime(formCollection["HireDate"]);
- employee.Salary = Convert.ToInt32(formCollection["Salary"]);
- _dbContext.Employees.Add(employee);
- _dbContext.SaveChanges();
- RedirectToAction("Index");
- }
- return View();
- }
- }
- }
Step 6

Code for Create View
- @model MvcFormCollection_Demo.Models.Employee
- @{
- ViewBag.Title = "Create";
- }
- <h3>Create New</h3>
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- <div class="form-horizontal">
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.DropDownList("Gender", new List<SelectListItem>
- {
- new SelectListItem { Text = "Male", Value="Male" },
- new SelectListItem { Text = "Female", Value="Female" }
- }, "Select Gender", new { @class = "form-control" })
- @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Position, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Position, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Position, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Office, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Office, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Office, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.HireDate, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.HireDate, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.HireDate, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
Step 7
The question is do we really have to write all the dirty code of retrieving data from FormCollection class and assign it to the properties of “employee” object? The answer is no. This is the job of the model binder in MVC.


Akshay DalviPosted Aug 13, 2021, 2:02 PM
Thanks Farhan !!
Akshay ShahaPosted May 20, 2019, 9:44 PM
Thanks Farhan.. very helpful for a newbie like me. ??
Cris GomezPosted May 20, 2019, 9:26 AM
Great tutorial... Thanks Farhan!