ASP.NET MVC Model Binding Example

Scott does an excellent job in his introduction blog post to the new features in the ASP.NET MVC beta release. The Model Binder support is an excellent feature for which I wanted to put forth a simple example. 

In essence, Model Binding allows you to pass an object into your controller methods rather than be required to pass the values of each property for your model that you intend to set within your Controller method. In retrospect, that description sounds pretty harsh and confusing, doesn't it? For now I'll spare you an introduction to Model Binding, Scott's already done an excellent job of that via these two posts

For this example I'll continue with Scott's Person class example. I'll have my Controller method, which takes instances of Person classes. The code below contains all of the Controller and Person Model code for this form-posting scenario. Pay special attention to the Save method, as it demonstrates usage of Model Binding; an instance of the Person class is passed into this method via a parameter named person

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace LearningMvcBeta
{
    namespace LearningMvcBeta.Controllers
    {
        public class PeopleController : Controller
        {
            public ActionResult Create()
            {
                return View();
            }
            public ActionResult Save(Person person)
            {
                return View(person);
            }
        }
    }
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Address Address { get; set; }
    }
    public class Address
    {
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
    }
}

 

Then (this is the part I think most examples won't be as clear on as I'm trying to be herein) I'll create my Create view HTML code. This will present the user with a form. Take note, the Person class parameter from the Save method above is named person, so the names of the HTML text elements have to be in the person.PropertyName format. If I'd named the Person parameter p, I'd want to name the HTML text elements using the p.PropertyName format. 

 

    
        Your Name
    
        
            First Name 
            
        
        
        
            Last Name 
            
        
        
        Your Address
        
        
            Street 
            
        
        
        
            City 
            
        
        
        
            State 
            
        
        
        
    
    

 

The image below is a screenshot I took of my debugger while testing this code with sample data. This is really powerful stuff - the child Address object defined by the instance of this Person class is sent in as well as was the First and Last name properties, because the Model Binding support trickles down the object graph!

Using the nomenclature MVC expects in your HTML form elements results in Model Binding automatically figuring out how to map the HTML form values to the parameter's properties just before the resulting object is fed into the controller method. 

This is one of my favorite new features of ASP.NET MVC, for sure. Thanks a lot, ASP.NET MVC overlords!

Update: Sorry if you've tried to find this post and received an Index out of range error. The syntax highlighter I've been using seems to have pooped the bed. So, I had to make a few changes to the supporting code and I think all's well now. 

Brady Gaster
Hi! I'm a christian dad who lives near Seattle, where I work with the talented folks at Microsoft to create compelling demonstrations for conferences that instruct and inspire developers who want to party in the cloud.