Upgrading our site to MVC RC

At work, we have a web application implemented using the ASP.NET MVC Framework.  This week, I upgraded that site from MVC beta to MVC Release Candidate.  The release notes had some important information about editing the web.config so that you don't need a code-behind for your views, but all it tells you about upgrading the rest of your code-base is that you should upgrade the MVC assemblies that your application is referencing and then fix the compiler errors.  Even after reading about the various changes in the RC, I still had a little trouble figuring out how to adapt our code to work with the various breaking changes, so I figured I would outline some of the issues I ran into here, and how I resolved them, to save you some time if you haven't gotten around to doing this upgrade yet.  Here goes...

  • ModelBinderResult no longer exists.  BindModel() should now take a ControllerContext as well as a ModelBindingContext parameter, and it should return the model itself, rather than wrapping it in a ModelBinderResult
  • IValueProvider went away - use FormCollection instead to retrieve form values
  • The value provider parameter to UpdateModel is now of type IDictionary<string, ValueProviderResult>, rather than IValueProvider (since IValueProvider no longer exists).  You must call ToValueProvider() on your FormCollection and pass that to UpdateModel.
  • ModelBindingContext no longer has a "Controller" property.  If you're trying to access that from the BindModel method in a model binder, access it from the new ControllerContext parameter instead.
  • ModelBindingContext.ValueProvider is now of type IDictionary<string, ValueProviderResult> (not IValueProvider since that no longer exists), so any places where you're calling GetValue() on that, you will need to replace it with a call to the indexer (eg. bindingContext.ValueProvider[fieldName] instead of bindingContext.ValueProvider.GetValue(fieldName)).  Also note that when you attempt to obtain a value from a generic Dictionary for a key that does not exist, you will get a KeyNotFoundException, whereas GetValue() simply returned null, so you will need to add a check that the key exists before retrieving a value from the collection.
  • The ModelBindingContext constructor now takes zero arguments instead of seven.  If you're instantiating a ModelBindingContext in a unit test, rather than passing everything in on the constructor, you'll want to set
    the ValueProvider, ModelType and Model properties (the controller is no longer set on the ModelBindingContext - its set in the ControllerContext, and all the other constructor parameters we were passing null for).
  • RedirectToRouteResult.Values is now RedirectToRouteResult.RouteValues

 

Good luck and God speed!

 

del.icio.us Tags: ,,,,

Print | posted @ Thursday, February 05, 2009 10:22 PM

Comments on this entry:

Gravatar # re: Upgrading our site to MVC RC
by software developer at 8/19/2009 10:34 AM

Cool,
excelent article, so well writen and useful
Thanks
Gravatar # re: Upgrading our site to MVC RC
by site messenger at 12/28/2009 4:49 AM

I want to express my admiration of your writing skill and ability to make reader to read the while thing to the end. I would like to read more of your blogs and to share my thoughts with you. I will be your frequent visitor, that’s for sure.
Gravatar # re: Upgrading our site to MVC RC
by Penny casino at 1/23/2010 4:29 AM

I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers Nice article.The code works fine and is a good starting point for me making a custom module.Thanks for the module..

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 2 and 1 and type the answer here: