Getting Posted ASP.NET MVC Form Data
page 5 of 8
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 58723/ 150

Handling Data with Parameter Mapping

Obviously the Request object is too complicated to be used for most of the interactions between the view and the controller. The ASP.NET MVC team of course built in some methods of obtaining data with strongly-typed access to the values posted by the form. When these values are posted back to the server they are inserted in to the controller by intelligently assigning the values based on the names given to the controls and the controller parameters. This allows MVC to pass the posted values into parameter variables used by the controller action.

To demonstrate this, we will use another controller action. This time we will use parameters to get access to our data, so we will have three parameters in our controller action this time: firsName, middleName, and lastName. We will perform our logical operations on these parameter variables.

Listing 5: Obtaining data from the parameters

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DisplayUserData2(string firstName, 
    string middleName, string lastName)
{
    ViewData["FirstName"= firstName;
    ViewData["MiddleName"= middleName;
    ViewData["LastName"= lastName;
 
    return View("DisplayUserData");
}

Notice that we now use strongly-typed parameters instead of the strings passed to the Request.Form variable. The ASP.NET MVC framework is handling the mapping of the values passed in the form data onto these controller parameter variables.

When observing the results we receive when using this controller action, we see a similar result.

Figure 8: Display view with parameter mapped data


View Entire Article

User Comments

Title: Nice explanation   
Name: Michael Rogers
Date: 2009-11-29 9:57:42 PM
Comment:
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
Comment:
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.
Regards
B.P.Mishra
Title: Nice Blog!   
Name: Software Testing India
Date: 2009-07-28 8:15:44 AM
Comment:
Thanks for this essential blog.

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-03-01 5:00:39 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search