ASP.NET MVC 2 Beta Default Parameter Values

Posted by: Malcolm Sheridan , on 11/26/2009, in Category ASP.NET MVC
Views: 26119
Abstract: The following article talks about ASP.NET MVC 2 Beta and the new DefaultValue attribute.
ASP.NET MVC 2 Beta Default Parameter Values
 
If you’ve been living under a rock in the last few days you may have missed one of the big announcements at PDC 09. Microsoft has released ASP.NET MVC 2 Beta! This is very exciting! To get your hands on this beta release you can go here to download it.
Some of the cooler features in this release are the following:

- Strongly typed UI helpers
- Areas support
- DataAnnotation validation support
- UI helper templating support

ASP.NET MVC 2 Beta works side by side with ASP.NET MVC 1. This article was creating using Visual Studio 2008 Service Pack 1. Once you’ve installed the beta you’ll have the option for both projects.

 

NewProject
 
I’ll focus more on the different areas in upcoming articles. This article will look at the new DefaultValue attribute. This is a way for specifying default values for parameters. In ASP.NET MVC 1 you either had to write a custom route to pass in default values, or create nullable types as parameters and check for the null values in your code. ASP.NET MVC 2 Beta now supports decorating action method parameters with the DefaultValueAttribute from the System.ComponentModel namespace. This allows you to specify parameters values if none are present. 
The above scenario is much simpler with an example. Suppose you build a website which displays a list of cars. You’ll need to create a cars controller. Each car has a make and model, but the user might not know the make and model, so this is when you would create a default route in the global.asax file to handle this:
routes.MapRoute(
                "CarsDefault",
                "Cars/{action}/{make}/{model}",
                new { controller = "Cars", action = "Model", make = "Holden", model = "Camaro" }
                );
 
routes.MapRoute(
                "Default",                                            
                "{controller}/{action}/{id}",                         
                new { controller = "Home", action = "Index", id = "" }
            );
The first MapRoute will default the make value to Holden and the model value to Camaro if no parameters are passed to the action method. There’s nothing wrong with doing it this way. The downside of this is your global.asax file can fill up quickly if you have numerous routes. In ASP.NET MVC 2 Beta this is taken care of for you thanks to DefaultValue attribute. Now you can leave the global.asax file and move the logic into your action method:
C#
using System.ComponentModel;
public ActionResult Make([DefaultValue("Holden")] string make,
                         [DefaultValue("Camaro")] string model)
{
return View();
}
The DefaultValue attribute is prefixed to the parameter, so in the example above, if I don’t specify a make, the default value will be Holden. This frees up the global.asax file and makes your code more discoverable in my opinion. Default values can be positioned anywhere in your arguments, so your action could look like this:
C#
using System.ComponentModel;
public ActionResult Make([DefaultValue("Holden")] string make,
                         string model)
{
return View();
}
 

This is one of the new features in ASP.NET MVC 2 Beta. In the next few weeks I’ll be posting more articles on the other new features. Stay tuned! The entire source code of this article can be downloaded over here

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles
Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by nnnnnnnnn on Friday, November 27, 2009 12:11 PM
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Comment posted by karthik on Monday, December 30, 2013 1:25 AM
Reducing the Routing complexity
Comment posted by karthik on Monday, December 30, 2013 1:25 AM
Reducing the Routing complexity

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel