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”