Home > ASP.NET MVC > ASP.NET MVC 5 Update

ASP.NET MVC 5 Update

September 24, 2008

Yesterday I wrote about updating to ASP.NET MVC Preview 5. Today I tested the changes I made and they failed, so I obviously had a little more work to do.

First, I had to update the Web.config file in several places (just like I did going from Preview 2 to Preview 3.) The only way I could get this to work was to create a brand new Preview 5 web page project and copy the afflicted lines from the new Web.config to the old one. I also went ahead and updated the namespaces, httpModules, and httpHandlers sections.

Then, with the site actually running again, I tried a full blown walk through and discovered a problem. Since System.Web.Mvc.BindingHelperExtensions was replaced with a call to UpdateModel(model, Request.Form.AllKeys), an error occurred. Specifically, a NullReferenceException was thrown because “AllKeys” contained form elements that are not properties in the model object. My gut reaction was to simply add them to my class, but that just didn’t seem right: the model should be unaffected the presentation.

So, at least as a temporary solution, I created a custom List<string> of Keys. I used Reflection to get a list of Property names, and if those names are in Requst.Form.AllKeys then I add it to my list:

typeof(TransactionRequestInfo).GetProperties().ToList().ForEach(p =>
        if (Request.Form.AllKeys.Contains(p.Name))

UpdateModel(pmtInfo, keys.ToArray());

{NOTE: TransactionRequestInfo and pmtInfo are from the Authorize.Net project, which I still need to update on the site.}

After I got it working, I realized that this is actually a query, so I could use Linq instead:

var keys = from p in typeof(TransactionRequestInfo).GetProperties()
           where Request.Form.AllKeys.Contains(p.Name)
           select p.Name;
UpdateModel(pmtInfo, keys.ToArray());

This shortened the code, got rid of a Lambda Expression, and prevented the need to cast the Properties[] to a List<PropertyInfo> object.

I should probably add logic to make sure only Public properties are selected. Since I may end up using this in more than one place, I will probably want to make it an Extension method of Request.Form.

One more problem

After updating my assembly references in Web.config and reloading the site, I received an error because it could no longer find the referenced assembly.  I checked in my bin folder and sure enough the assemblies were not there.  Compiling the solution did not replace them as it had before, so I copied them into bin from the C:\Program Files\Microsoft ASP.NET\ASP.NET MVC CodePlex Preview 5 directory and reloaded the site.  Once again, all is working properly.

Categories: ASP.NET MVC
%d bloggers like this: