Custom Model Binder and More UI Validation in ASP.NET MVC

Previous Tutorials

In the recent ASP.NET MVC Tutorials I have been investigating a few of the new features in ASP.NET MVC Preview 5. I mentioned the ComplexModelBinder for help in binding your form values with a custom class in your model and now I want to show an example of using a Custom Model Binder in case the Default and ComplexModelBinder don't meet your needs. Although I am going to show a simple custom model binder, you can of course adapt it to your needs.

 

Registering a Custom Model Binder

You can register a custom model binder in your Global.asax Application Start. In this case we are going to register a custom model binder for our Customer Class:

 

protected void Application_Start()
{
    ModelBinders.Binders[typeof(Customer)] =
        new CustomerBinder();
}

 

The Customer Class is far from interesting and only has 3 simple properties for our trivial example:

 

public class Customer
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}

 

Creating a Custom Model Binder

The Custom Model Binder for the Customer Class will implement the IModelBinder Interface and instantiate a Customer instance and populate its properties from Request.Form.

 

public class CustomerBinder : IModelBinder
{
    public object GetValue(ControllerContext controllerContext,
        string modelName, Type modelType,
        ModelStateDictionary modelState)
    {
        Customer customer = new Customer();

        customer.Name = controllerContext.
            HttpContext.Request.Form["Name"];
        customer.Email = controllerContext.
            HttpContext.Request.Form["Email"];
        customer.Phone = controllerContext.
            HttpContext.Request.Form["Phone"];

        return customer;
    }
}

 

Custom Model Binder In Action

The Custom Model Binder will be executed in the background for you when you add the Customer Class as your argument in your Controller Action. For kicks, I check that the value of the Email property on Customer is a valid email. If it isn't, I add an appropriate error to the ModelState so that the UI displays a validation error appropriately. Yes, there are better ways to handle validation and I will get to those in a separate post :)

 

public ActionResult Save(Customer customer)
{
    var regex = new Regex(@"[\w-]+@([\w-]+\.)+[\w-]+");
    if (!regex.IsMatch(customer.Email))
        ViewData.ModelState.AddModelError("Email",
            customer.Email, "Dude, Email is wrong!");

    return View(customer);
}

 

The UI displays the error accordingly:

 

Custom Model Binder

 

Conclusion

Custom Model Binders can get much more interesting than the one above. Hopefully this example and the ComplexModelBinder Example may have helped you better understand some of the features in the ASP.NET MVC Framework.

 

David Hayden

 

posted on Monday, September 08, 2008 12:05 AM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices