Getting Posted ASP.NET MVC Form Data
page 4 of 8
by Brendan Enrick
Average Rating: 
Views (Total / Last 10 Days): 49493/ 40

Handling Data with Request.Form

For those familiar with the Request object, the answer is yes, this is the same request object. It gives us access to the values posted to the server. We will be using these to extract the information we want to display to the user and then we will be storing this in the ViewData. The ViewData is where we get access to information in the view.

To start we need to make a method in the controller because that is basically all the controller actions are. We will name it DisplayUserData1 since that is what we said the action was called from our form view. The method will return the view we want to render. In this case it is called DisplayUserData. Once we do this we should have this code.

Listing 3: Simple controller action

public ActionResult DisplayUserData1()
    return View("DisplayUserData");

Now when we go to view the page we should see the following result.

Figure 6: Empty display view

The obvious reason why everything is blank is that we tried to fill everything based on the values obtained from ViewData, but we have not actually put any data into the ViewData object. Now we need to fill some data in so we can see a better result. We need to get this data from the posted data.

We are going to use the Request object first. So we use the indexer on the Request.Form object and pass it the name of the control whose value we want to obtain. So we want to use the names of the three text boxes we used on the first view.

Listing 4: Obtaining data from the Request.Form directly

public ActionResult DisplayUserData1()
    string firstName = Request.Form["FirstName"];
    string middleName = Request.Form["MiddleName"];
    string lastName = Request.Form["LastName"];
    ViewData["FirstName"= firstName;
    ViewData["MiddleName"= middleName;
    ViewData["LastName"= lastName;
    return View("DisplayUserData");

When we submit the information, we receive the following output. We see the data displayed back to us as we expect. This shows that we successfully submitted and obtained the submitted data.

Figure 7: Display view with Request.Form data

Alternate Access to the Form Data

If you do not want to use the classic method of accessing the Request object to get to the posted form data, you can instead use the new and improved "dependencies up front" way. I call it this because the dependency on the form will be shown to anyone using the controller. In this we add a method parameter to the controller action which is of type FormCollection. ASP.NET MVC will then pass the data into that parameter variable when calling the action.

This is equivalent to what you have seen using the Request.Form collection. I kept this as the alternate because the first way of seeing it is more familiar to ASP.NET developers. I prefer using this method if I need access to the FormCollection object because it keeps my dependency on the data obvious. Working with the collection in this manner also makes it easier to fake the data in the case of testing and overall makes the data more maintainable.

View Entire Article

User Comments

Title: Nice explanation   
Name: Michael Rogers
Date: 2009-11-29 9:57:42 PM
Thank you for providing such a clear and succinct explanation.
Title: Very informative and well compiled.   
Name: B.P.Mishra
Date: 2009-08-03 5:08:48 AM
Dear Brendan
This article gave me new idea about the MVC framework as I am in the process of learning it.Thank you.keepup the goodwork.
Title: Nice Blog!   
Name: Software Testing India
Date: 2009-07-28 8:15:44 AM
Thanks for this essential blog.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023  |  Page Processed at 2023-06-06 6:20:23 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search