Introduction
In MVC, when we need to collect all the form values in an action method of a controller, there are several ways through which we can receive form post data in Controller.
Different Ways
I will cover the below 4 ways through which I can receive form Post data in Controller:
- Strongly Typed Model
- Request Object
- Form Collection
- Through Parameters
Strongly Typed Model
This is one of the very common approaches which we use in MVC. We can directly receive form values through a model in controller. In this approach, we bind a view directly from a model and when we post the form, we can receive all the form data in an object of that model as a parameter in the post action method.
Example: Suppose I have a StudentModel
in my project as below:
public class StudentModel
{
public string Name { get; set; }
public string Email { get; set; }
public string PhoneNo { get; set; }
public string Address { get; set; }
}
I have a controller named StudentController
where I have an action method named StudentRegistration
with HttpGet
verb as below:
public class StudentController : Controller
{
[HttpGet]
public ActionResult StudentRegistration()
{
return View();
}
}
Right click on the Action
method and select create View and while creating a view, select strongly Typed view as below:
Once we click on add, we will get a view inside Views/Student folder named StudentRegistration.cshtml.
Now, create another action method with httpPost
verb where we will post our form as below:
Request Object
Suppose we don't want to use Strongly type Model and we are creating our view without the help of the Model. Suppose if we are creating our StudentRegistration
view as below, then we will not be able to receive post data in model Object. In that case, we need to choose a different approach to get the post data in action method.
We created an Action method named StudentReg
where we will post the form and there, we can receive the form post data through Request
object as below:
public class StudentController : Controller
{
[HttpGet]
public ActionResult StudentRegistration()
{
return View();
}
[HttpPost]
public ActionResult StudentReg()
{
string name = Request["txtName"].ToString();
string email = Request["txtEmail"].ToString();
string phoneNo = Request["txtPhoneNo"].ToString();
string password = Request["txtPassword"].ToString();
return View();
}
}
Form Collection
This is another way to get the post data in controller. We can get the post data through Form collection as well. Suppose we have a view as in the above example and we want to get all the post data in Controller, then we will use Form Collection as below:
public class StudentController : Controller
{
[HttpGet]
public ActionResult StudentRegistration()
{
return View();
}
[HttpPost]
public ActionResult StudentReg(FormCollection col)
{
string name = col["txtName"].ToString();
string email = col["txtEmail"].ToString();
string phoneNo = col["txtPhoneNo"].ToString();
string password = col["txtPassword"].ToString();
return View();
}
}
So we can receive values through Form Collection as well and it's again very easy.
Through Parameters
We can receive post data directly in parameters of an Action method. Parameter Name should be the same as the id of the control. For example, if Id of a text box on a view is txtName
then Parameter name should be txtName
. Here is an example
public class StudentController : Controller
{
[HttpGet]
public ActionResult StudentRegistration()
{
return View();
}
[HttpPost]
public ActionResult StudentReg(string txtName,string txtEmail,
string txtPhoneNo,string txtPassword)
{
return View();
}
}
On StudentRegistration
View, we have Name
, Email
, PhoneNo
and Password
text boxes having id txtName
, txtEmail
, txtPhoneNo
and txtPassword
.
Note: Control ID should be the same but we can pass parameters in any order, i.e., we can write txtEmail
before txtName
.
At last - If this is helpful and you liked this, please vote.