March 5, 2021
Hot Topics:


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

To specify the model for the view, you need to modify the view page's declaration. For example, for the Index.aspx view, you would edit the code-behind file Index.aspx.cs, and specify a mode type for the view:

public partial class Index : ViewPage<Models.Product>

By default, each view page descends from the ViewPage class and doesn't have any generic parameter types. But, by appending the desired model type in angle brackets, you can specify the type for the model that the view understands. Only one type can be specified here.

Once you have this declaration in place, you can directly access the model class' properties. This happens through the ViewData object's Model property like this (see Figure 5):

<p>Product ID: <%= ViewData.Model.Id %></p>

Figure 5: IntelliSense is available to help you once you define the model type.

As you can see from the listing, you can use the model's properties very easily in the .aspx page. Of course, specifying the model does not limit your ability to use the ViewData object in the regular way as a data dictionary. Being able to use the model is just an additional benefit.

The final piece of the model puzzle is "How does the view page know which Product object's data to display?" As you might guess, this is specified in the controller. When returning the view object, you can pass in an instance of the desired model object. Then, the view would use this particular instance when accessing the model's properties.

To specify the model object instance in the controller, you would modify the return statement slightly:

Models.Product product = new Models.Product();
product.Id = 123;
// ...
return View(product);

Note that the View method has multiple overloads, and passing the model object is one of them. It is recommended that you store all model classes inside the "Models" folder of the solution, but this is not mandatory. It should also go without saying that if your application uses databases, the controller would be the place to access it and store information to the appropriate model objects.

Please return to Developer.com on December the 10th for a follow-up article, "Accepting Input and Manipulating Data in ASP.NET MVC." In that article, you will learn how ASP.NET MVC applications map URL parts into controller method parameters, and how you can process those parameter values. Furthermore, you will also see how to manipulate database data, and effectively accept input from the user.


ASP.NET MVC is an interesting new way to develop web applications in the .NET world. It clearly raises the level of application development from mundane HTML and request/response processing to a higher stage.

Microsoft has used lots of effort to make ASP.NET MVC easily programmable, and, for example, the ability to use strongly typed methods is just one example. Although the usage of regular HTML controls versus regular ASP.NET server-side controls can cause confusion, the value of the MVC pattern is cleaner code.

In this article, you learned the basics of ASP.NET MVC applications: how they live and breathe. Testing the framework yourself is easy as installing the latest beta in your development machine, and starting a new MVC web application project. By following this article, you should have a head start to the new framework.

As it goes, the excuses for not using ASP.NET MVC are getting thin. It's time to learn a new technology before Christmas!

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 and is 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 4 of 4

This article was originally published on December 3, 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