Complex Object Model Binding in ASP.NET MVC

In the weeks since my last post, I’ve been doing more client-side work and re-acquainting myself with ASP.NET MVC model binding.  The default model binder in ASP.NET MVC works extremely well.  In the applications I’ve worked on over the past 2 1/2 years, there have been maybe a couple of instances where the default model binder didn’t work the way I needed. The problems I’ve encountered with model binding lately have had more to do with read-only scenarios where certain data still needs to be posted back.  In the Razor template, I’ll have something like the following:

@Html.LabelFor(m => m.Entity.Person, "Person: ")
@Html.DisplayFor(m => m.Entity.Person.Name)
@Html.HiddenFor(m => m.Entity.Person.Name)

Nothing is wrong with the approach above if Name is a primitive (i.e. string).  But if I forgot that Name was a complex type (as I did on one occasion), the end result was that no name was persisted to our datastore (RavenDB) which meant that there was no data to bring back when the entity was retrieved.  The correct approach for leveraging the default model binder in such cases is this:

@Html.LabelFor(m => m.Entity.Person, "Person: ")
@Html.DisplayFor(m => m.Entity.Person.Name)
@Html.HiddenFor(m => m.Entity.Person.Name.FirstName)
@Html.HiddenFor(m => m.Entity.Person.Name.LastName)
@Html.HiddenFor(m => m.Entity.Person.Name.Id)

Since FirstName, LastName and Id are all primitives, the default model binder handles them appropriately and the data is persisted on postback.

App_Offline.htm

I came across a couple of useful posts from Scott Guthrie about App_Offline.htm. This page appears and disappears automatically when the “Publish Web Site” option is used. What I didn’t know is that it’s part of the .NET framework (not the IDE). This means the page can be added and removed manually. This will be especially useful in my current environment, where we depend on network engineers to deploy web applications to test and production sites.

Here are the posts:
App_Offline.htm and IE Friendly Errors

Announcing App_Offline.htm

ASP.NET Configuration File Handling

One of things I like the least about working with multiple development, QA, and production environments is messing around with configuration files to make sure the different versions point at the right databases.  Add the use of the Enterprise Library, and there are even more files to manage.

In my last year at Ciena, I worked in a group where they’d put together some code that detected what environment it was in (development, QA, or production) and retrieved the correct settings from web.config.  It was similar to the solution Mike Gunderloy describes in this article on ASP.NET 2.0 productivity.

When I was poking around for more information on config file handling, I came across another article that references Gunderloy’s that talks about a file attribute for the appSettings tag.  I hadn’t come across the existence of that attribute anywhere else before.  I’ll definitely use this on my next project.

Finally, this article provides another option for dealing with configuration files in multiple environments.  The code and examples are well-explained.  My group at Lockheed Martin should integrate something like this into the custom library we’ve been building.

“Free” Project Management Software

I was looking for information on the right way to modify the ASP.NET Issue Tracker starter kit to handle Windows Authentication. One of the things Google search returned was an app called Gemini that does a similar thing. Up to 10 people can use the version they offer for download before they start asking for money. Considering the way the current IssueTracker installation is behaving right now, I’m beginning to wish I’d found Gemini earlier.