Default Model Binding Behavior in MVC2

This article explains the key concepts to be aware of with model binding in MVC2.

When an HTTP request is made to the server, request routing directs it to a Controller for execution. The Controller collects HTTP values in the request and loads them into a ModelState dictionary, which is used throughout the MVC request lifecycle. In this article, various ways in which ModelState can be consumed within the Action are described. ModelBinders provide the logic for converting the ModelState data into simple and complex types variables. The default ModelBinder used by MVC is very versatile.

Continue reading “Default Model Binding Behavior in MVC2”

Using MVC2 TempData with the PRG Design Pattern

Post/Redirect/Get (PRG) is a common design pattern recommended for use with HTML forms. Redirecting after a successful form post can help avoid accidental duplicate submissions and allows a browser to behave more intuitively with bookmarks and the refresh button. Most MVC examples follow this pattern:

public ActionResult Edit(int id, Contact contact) {
    if (ModelState.IsValid) {
        // Submit values to the database
        return RedirectToAction("Index");
    return View();

As you can see, if the POST action is completed successfully, then a REDIRECT occurs to GET a new page. If there is a validation error, then the posted data is returned back to the client through a view (usually through the same view). ViewData keeps track of posted data throughout the request lifecycle. You can add and modify values in this collection during action processing. However, when a RedirectToAction is executed, the ViewData is lost. This is a good thing since the redirect causes a new GET request to be issued which, according to RESTful principles, should not track any state from a preceding request.

However, sometimes we want to internally pass information to the next request. This could be implemented as a URL parameter, but then the information would be present whenever that specific URL was requested rather than just one time in the next request after the post that issued the redirect. A common need, for example, is to display a confirmation message after an update from the previous page has completed successfully.

Continue reading “Using MVC2 TempData with the PRG Design Pattern”

Building Forms with MVC2 and EF4

I’ve always thought that working with forms in ASP.NET was a bit messy, but with the recent launch of Entity Framework 4 and MVC 2 (both part of Visual Studio 2010 / .NET 4), there is some really nice functionality to take advantage of. I got off to a great start by reading through Scott Guthrie’s NerdDinner sample to get a feel for MVC concepts. However, his sample is based on MVC 1 and used LINQ-to-SQL rather than EF, so I found that quite a bit of the information wasn’t applicable.This post shares some points on building MVC 2 forms more effectively using the Entity Framework. A few things that I wanted to get right in particular were business rule validations for form elements, use of foreign key associations within the form, and strong typing so that errors are caught at compile time rather than at run time.

This post demonstrates the creation of a form for creating and editing an individual Product. The Product object has a VendorId property that relates to a Vendor object.

Continue reading “Building Forms with MVC2 and EF4”

Better Looking Documents in Google Docs

I use Google Docs on a regular basis, and decent looking documents are important to me. Not fancy, just professional.

The document editor in Google Docs is decent enough for productive use. It has a bit of friction though; it’s easy to create incorrectly formatted text, but it can be hard to fix the mistake.

This post distills some knowledge I’ve gained, and techniques I’ve adopted, for maximizing my productivity when writing Google documents.

Continue reading “Better Looking Documents in Google Docs”

“Tabify” – the No Markup jQuery Plugin

I needed to introduce a tabbed layout for displaying details of certain products within a web-based e-commerce product we use.

The product includes a WYSIWYG editor, which enables product details to be entered without any (or at least limited) HTML knowledge. The problem is that quite a bit of additional markup was needed to implement the tabbed layout, and that meant that our business users would have difficulty maintaining the content accurately.

To solve the problem I created a jQuery plugin for converting content into a tabbed layout without needing to manually inject additional markup for formatting. Here’s how it works:

Continue reading ““Tabify” – the No Markup jQuery Plugin”

Version Control Software: From VSS to Mercurial

Until a couple of years ago, I used VSS for source code version control. VSS is not a very capable or reliable product, but the licensing cost for Team Foundation Server prevented me from switching to Microsoft’s newer technology.

Then I switched to Subversion, using VisualSVN for Visual Studio integration. At only $49 for each client, and the standard edition server component at no cost, this has proved to be a really good improvement over VSS. We have been very happy with Subversion.

In the past year or so, there has been a lot of hubbub about Distributed Version Control Systems. Armed with a basic understanding of the unique “distributed” nature of these systems, I could see the value for large team environments, but I had a hard time appreciating the increased value offered to the smaller corporate or community team.

I learned enough to know that there were two main players out there – Git and Mercurial. They both have developed great reputations, but for development scenarios that I’m likely to be involved in, Mercurial seems like the better choice. The main reason is that it’s simpler to use. Version control can be complex to manage, and only works as well as the lowest common denominator in your team’s understanding of the version control product workflow and behavior.

So, if you’re trying to decide what Source Control System to use, consider giving Mercurial a try. Here are the main resources you’ll want to check out:

Let me know your experience if you decide to try out Mercurial.