Tag Archive: ASP.NET MVC


I wrote this library so I don’t have to keep track if I should use HttpRuntime.Cache, AppDomain.CurrentDomain.SetData, HttpContext.Current.Session, or a Dictionary…now I just use GlobalCache or UserCache.  It also makes unit testing a webapp a snap, because if you use IGlobalCache/IUserCache in your code, you just inject the Windows-based cache in your unit test setup.

You can download the solution with dlls here: Universal Caching solution

If you upgrade from MVC 1 to 2 or higher and your controller factory (such as StructureMapControllerFactory) throws a “no suitable method found to override” error for your GetControllerInstance method, be sure you have a reference to System.Web.Routing:

using System.Web.Routing;

And be sure you add the RequestContext parameter to the GetControllerInstance method call like this:

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)

Magic strings are just nuclear-powered bug magnets. They attract fat-fingering mistakes while coding and practically guarantee bugs will be introduced while refactoring. If you’re like me, you’ll hate the magic strings required in the Html.DropDownListFor as much as I do. Now there’s an easy way to get rid of them once and for all…

View full article »

Do you have a post action that accepts a parameter (and what post action doesn’t?), but you also have a get action that accepts a parameter and you are seeing that submits are going back to the get action and other strange behavior? Here’s how you fix that:

View full article »

Download a complete solution for this article here

Using ASP.NET MVC 3 and jquery, we can easily and automatically validate and format data such as phone numbers and social security numbers into user friendly formats like (xxx) xxx-xxxx or xxx-xx-xxxx, not only when data is presented to the user, but also while the user is typing in data! We can also block the user from even entering non-numeric characters if we wish. We can parse that data after a form submit and strip out the parentheses and dashes before it reaches the controller action. We can also easily control the appearance of the input textboxes of all the fields in all the forms in one easily modified location. What’s nice about the approach presented here is that the developer only needs to designate a model field as a social security or phone number in one location (the view model) and the system handles all the html, jquery, and data cleanup automagically. This is done without using the class html attribute on any of the input controls, which frees the developer to use css classes as normal. Furthermore, this approach is compatible with Microsoft’s jquery unobtrusive client-side validation, so you can present the user with friendly highlighting and popups before invalid data is even sent to the server.  Put together, these techniques create a crisp, fluid experience for the users and a fast, easy development environment for the developers.

View full article »

© 2012 Robert Corvus