Click here to Skip to main content
15,121,641 members
Articles / Programming Languages / C# 5.0
Article
Posted 20 Jun 2016

Stats

17.8K views
17 bookmarked

Different Ways for Getting Post Data from View in Controller

Rate me:
Please Sign up or sign in to vote.
4.68/5 (34 votes)
20 Jun 2016CPOL2 min read
In this article, we will see different ways through which we can recieve post data by view in controller.

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:

  1. Strongly Typed Model
  2. Request Object
  3. Form Collection
  4. 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:

C#
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:

C#
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:

Image 1

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:

Image 2

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.

Image 3

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:

C#
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:

C#
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

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

VijayRana
Technical Lead
India India
Hi Myself Vijay having around 7 years of experience on Microsoft Technologies.

Comments and Discussions

 
SuggestionGood article for beginners Pin
Yoqueuris13-Jun-17 4:25
MemberYoqueuris13-Jun-17 4:25 
QuestionVery nice article Pin
SweetSSS30-Jun-16 22:35
MemberSweetSSS30-Jun-16 22:35 
SuggestionNo need for "ToString" Pin
Richard Deeming30-Jun-16 9:04
mveRichard Deeming30-Jun-16 9:04 
GeneralMy vote of 3 Pin
sunil kumar meena22-Jun-16 10:55
professionalsunil kumar meena22-Jun-16 10:55 
Praiseclear and helpful Pin
djklord22-Jun-16 5:07
Memberdjklord22-Jun-16 5:07 
PraiseMy vote of 5 Pin
Sha-Pai Li21-Jun-16 20:44
MemberSha-Pai Li21-Jun-16 20:44 
PraiseNice Pin
Praveen Kumar Katiyar20-Jun-16 22:04
professionalPraveen Kumar Katiyar20-Jun-16 22:04 
GeneralMy vote of 5 Pin
saurabh.btech.it20-Jun-16 21:40
Membersaurabh.btech.it20-Jun-16 21:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.