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

Handling Data with Model Binders

We know that the ASP.NET MVC Framework is smart enough to translate our FirstName TextBox into the firstName string parameter in our controller action. This is some very powerful stuff that has been added into the framework. This gets better though because the framework also allows for full object mapping using model binders. This will take the individual parameters and construct a full data model object based on the data.

In our example, we have the values required for a User object. Since this object does not yet exist, we will need to create it. So to start off this section of the article, we will create a basic class which we will use to model the data.

Listing 6: Simple User data model object

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DisplayUserData3(User user)
{
    ViewData["FirstName"= user.FirstName;
    ViewData["MiddleName"= user.MiddleName;
    ViewData["LastName"= user.LastName;
 
    return View("DisplayUserData");
}

Using the model binder allows us to skip the step of assigning the parameters to our data object. Most of the time we want to work with more advanced objects than the primitive types often passed by the simple form controls. Also, the User object is very nice to just obtain. If our user object had more properties associated with it, we would be quite glad to have the one parameter as opposed to having half a dozen parameters to deal with in our controller action.

It is possible to create custom model binders to deal with more complex situations than this one. In fact one of the most powerful assets available to the savvy ASP.NET MVC developer is the ability to customize the framework to suit specific needs. There are interfaces to implement and base classes to inherit which allow for a great deal of customization. This comes in handy when using inversion of control to invert the dependencies of your code, which is very common when working with controllers in ASP.NET MVC.

Once we execute the code using this action in our HTML form, we receive the same result we did before. We obtain the same view of the data, which shows us that these methods are fairly interchangeable. There are circumstances for each.

Figure 9: Display view with model bound 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-08 7:55:30 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search