5 Comments

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. 

 

<form action="/People/Save" method="post">
    
        <b>Your Name</b>
    
        <label for="FirstName">
            <span>First Name</span>
            <%=Html.TextBox("person.FirstName") %>
        </label>
        
        <label for="LastName">
            <span>Last Name</span>
            <%=Html.TextBox("person.LastName")%>
        </label>
        
        <b>Your Address</b>
        
        <label for="Street">
            <span>Street</span>
            <%=Html.TextBox("person.Address.Street")%>
        </label>
        
        <label for="City">
            <span>City</span>
            <%=Html.TextBox("person.Address.City")%>
        </label>
        
        <label for="State">
            <span>State</span>
            <%=Html.TextBox("person.Address.State")%>
        </label>
        
        <input id="submit" type="submit" value="submit" />
    
    </form>

 

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. 

Comments

Comment by fsadf f

hi

Comment by dantegt

The Image is not available any more :(

Great article, simple and concise.

dantegt
Comment by tt

dddd

Comment by tt

not good artical

Comment by Property management companies san francisco

San Francisco is a beautiful city that affords many wonderful sights. However, in this economy traveling to places such as this means high hotel rate that prohibit the chance venture there. But with San Francisco vacation rentals, you can see the city and save a buck.

Post comment