January 21, 2021
Hot Topics:

Accepting Input and Manipulating Data in ASP.NET MVC

  • By Jani Järvinen
  • Send Email »
  • More Articles »

If you decide to go the route of a separate action handling the form, you actually have two options to get the data back from the form: using a controller method called UpdateFrom, or by using model binders.

With the UpdateFrom method, you pass in an object instance with public properties, and the method then looks at the form fields and sets the object's properties accordingly. This way, all HTML form fields can be processed easily. Optionally, you can pass in an array such as Request.Form.AllKeys to specify which properties you want to update. For instance:

public ActionResult Update()
   Models.Product product = new Models.Product();
   UpdateModel(product, Request.Form.AllKeys);
   return View();

Although the UpdateFrom method works, it has its problems. For example, properties with types other than strings can cause trouble, and the programming pattern looks more or less like a black box. The cleaner approach is to let the MVC framework bind the model back to a strongly typed parameter value in the action method.

To do this, you would simply declare the action method processing the form like this:

public ActionResult Update(Models.Product product)

Given this declaration, the MVC framework knows that you expect a strongly-typed model. With a method declaration like this in place, the framework automatically looks for HTML form fields with the name "parametername.property"; for instance, "product.Id" and "product.Name". This would mean that you would have to edit your HTML form generation code slightly:

<%= Html.TextBox("product.Id", ViewData.Model.Id) %>
<%= Html.TextBox("product.Name", ViewData.Model.Name)%>

Note that the HTML field names are not case sensitive, so either "ID" or "Id" will do. Also, you can test at runtime if the controller's Model.IsValid property is true to see whether the data entered by the user conformed to the model object's property types. For instance, if an invalid numeric string is entered, Model.IsValid will be false.


ASP.NET MVC provides a clean separation among the user interface, application logic, and data presentation. To use this new technology efficiently, you need to learn how to do things the MVC way.

In this article, you learned how ASP.NET MVC applications map URL parts into controller method parameters, and how you can process those parameter values. Furthermore, you also saw how to manipulate database data, and effectively accept input from the user.

The new Beta 1 release of ASP.NET MVC also contains an explicit "go-live" license; this means that, if you want, you can put your ASP.NET MVC web application into production today. Microsoft has also engineered MVC Beta 1 so that it is easy to deploy: by copying the MVC assemblies (DLLs) into your web application's Bin subfolder, you can run ASP.NET MVC application on web servers that do not have MVC assemblies preinstalled (see Figure 1).

Click here for a larger image.

Figure 1: Deploying to a production environment is well documented from the ground up.

Good luck with this new .NET web application model!

Jani Järvinen


The following links help you get started with ASP.NET MVC.

About the Author

Jani Järvinen is a software development trainer and consultant in Finland. He is a Microsoft C# MVP, a frequent author, and has published three books about software development. He is the group leader of a Finnish software development expert group named ITpro.fi. His blog can be found at http://www.saunalahti.fi/janij/. You can send him mail by clicking on his name at the top of the article.

Page 2 of 2

This article was originally published on December 10, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date