Archive for January 2008


I’ve been creating several shared components that I use throughout the MVC application.  I found that almost all my forms needed some kind of feedback mechanism on whether the form was successful or not.  I created an ErrorDisplay ViewUserControl which is called on most form input pages using the MVC HTML helper:  <%= RenderUserControl(“~/Views/Shared/ErrorDisplay.ascx”) %>.


This simple control will check for a key in the TempData dictionary and if it finds it, it will loop through the items in that dictionary.  The item I store in the TempData is a dictionary of error messages added from the controller.  My issue in this case was that I kept getting a null reference exception when trying to access the TempData from the user control.


On view pages, to access the TempData dictionary you can go through the ViewContext property directly.  However, from a view user control, we have to access it through the Html.ViewContext.TempData property.  This seems like a bug to me which I’m sure will be fixed in the next drop of the MVC framework.  Also, I believe that TempData will also be accessible directly as a property of the View Page in future releases.

Hope this helps! 

This weekend, Saturday, February 2nd is the South Florida Code Camp.  I’m really looking forward to some of the sessions.  I couldn’t believe that the ASP.NET MVC Framework got it’s own track on the agenda!  This is great news and shows how much excitement there is about the upcoming framework.  I’m looking forward to hearing what others are saying about it. 

On this note, I’m planning to put together a series of posts about my experiences with the ASP.NET MVC framework so far.  Mostly little tid bits and pitfalls I’ve hit along the way and how I was able to resolve it.  I hope to get some feedback from the community and perhaps build a “best practices” archive for working with the ASP.NET MVC Framework.  Look for it in the coming days.


TempData only persists over one request. 

The typical web application development process for me starts with finding a really great site template. Hopefully, it will include at least a couple of layouts, all the images, and very tidy stylesheets. So now I start building the application and inevitably start tweaking CSS classes, adding new ones, renaming, deleting and pretty much pillaging the entire template. Then I start adding my own javascript, AJAX, and more flashy UI stuff. Pretty soon, I’m working 90% on the UI and the rest actually developing the application.

jQuery makes all this “flashy” stuff a piece of cake, even if you don’t have a lot of javascript experience. It even allows advanced features like posting forms using AJAX, handling responses and anything else you can imagine. The most attractive feature is how easy it really is to implement. In just 2 lines of code you can get some amazing results.

I don’t see jQuery mentioned often in the ASP.NET community, so hopefully this will incite some exploration. There is the core jQuery API as well as hundreds of plugins which you can use with it. Its got huge community support and it’s open source.

Check out some of the UI plugins for it here to get a feel for it. View the source of the demos to see now little javascript it really requires. You’ll also want to browse the main page for more info and to start getting used to the syntax.

I plan to have more posts specifically about integrating jQuery with the ASP.NET MVC Framework.

Hope this helps!

When creating an application that requires a person’s age, we typically store the date of birth and calculate their age in years from that.  I usually store my user’s date of birth in a property of type DateTime.  To make it easy to calculate the user’s age based on this property, I decided to take advantage of .NET’s new extension methods feature.  By adding an extension method to .NET’s native DateTime type, we can quickly calculate the age of any object that has a DateTime property. 

We will create two extension methods.  One will accept no parameters and return the age based on the current date.  The overload of this method will accept an future date to base the age calculation on.  This would be useful if you wanted to know what the person’s age will be on some future date.  As we see below, they are very straightforward.

Once we have that, we just need to import the namespace that our extension methods are in.  All your DateTime types will now have this method available via intellisense.

To see the effect of our new extension method, let’s see what Jim Morrison’s current age would be followed by how old he’ll be in a number of days.  Finally, let’s see how long ago the US declared independence from the British.

The previous code will produce the following output: 


If you want to find out more about authoring your own extension methods, check out this article

Hope this helps.