ASP.NET MVC 2 Beta Default Parameter Values

Posted by: Malcolm Sheridan , on 11/26/2009, in Category ASP.NET MVC
Views: 33474
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

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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




Feedback - Leave us some adulation, criticism and everything in between!
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

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook