Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET MVC Model Binders

0.00/5 (No votes)
20 Jul 2011 1  
What Model Binders are in ASP.NET MVC and how you can use them in your MVC applications.

Introduction

One thing that I’m always asked to talk about when I’m delivering ASP.NET MVC sessions or courses is Model Binders. In this post, I’ll explain what Model Binders are in ASP.NET MVC and how you can use them in your MVC applications.

ASP.NET MVC Model Binders

Model Binders

ASP.NET MVC provides a way to de-serialize complex types that are part of the incoming HTTP request input. After these types are de-serialized, they are passed to the relevant controller’s action as method arguments. In the following controller action method code, during the execution of ASP.NET MVC pipeline, a binder is used to create the Customer instance from the request input:

C#
[HttpPost]
public ActionResult Edit(Customer customer)
{
    if (ModelState.IsValid)
    {
        var mycustomer = _customerdb.GetCustomer(customer.Id);
        TryUpdateModel(mycustomer);

        return RedirectToAction("Index");
    }

    return View(customer);
}

ASP.NET MVC supplies some binders out of the box such as DefaultModelBinder that uses conventions (such as naming conventions) in order to create the Model from the request, or FormCollectionModelBinder that maps a request to a FormCollection object. You can use those defaults, and of course you can create your own Model Binders.

Creating a Custom Model Binder

In order to create a Model Binder, you need to get familiar with the IModelBinder interface:

C#
public interface IModelBinder
{ 
    object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext); 
}

This interface declares the BindModel method which is the only method that you need to implement. The method gets as arguments the controller context and the model binding context. The ControllerContext object holds information about the current HTTP request and about the associated controller. The ModelBindingContext holds information about the model itself and its metadata. Most of the time, you won’t have to write your own binder but if you need to, then follow the example.

Custom Model Binder Example

In the example, I’ll use the following Course presentation model object:

C#
public class Course
{
    #region Properties
    
    public int CourseID { get; set; }
    public string Title { get; set; }
    public string Days { get; set; }
    public DateTime Time { get; set; }
    public string Location { get; set; }
    public int Credits { get; set; }

    #endregion
}

Here is a simple implementation of a Model Binder for the Course model:

C#
public class CourseModelBinder : IModelBinder
{
  #region IModelBinder Members

  public object BindModel(ControllerContext controllerContext, 
                ModelBindingContext bindingContext)
  {
    HttpRequestBase request = controllerContext.HttpContext.Request;
    int courseId = Convert.ToInt32(request.Form.Get("CourseID"));
    string title = request.Form.Get("Title");
    string days = request.Form.Get("Days");
    DateTime time = Convert.ToDateTime(request.Form.Get("Time"));
    string location = request.Form.Get("Location");
    int credits = Convert.ToInt32(request.Form.Get("Credits"));

    return new Course
    {
      CourseID = courseId,
      Title = title,
      Days = days,
      Time = time, 
      Location = location,
      Credits = credits
    };
  }

  #endregion
}

The binder will create the instance of the course by converting all the relevant types from the incoming HTTP request.

Remark: The DefaultModelBinder will also do the same thing since it will use the naming conventions and the property types in order to make the conversion. This is only a simple example in order to explain the concept of Model Binders.

Integrating a Custom Model Binder

In order to integrate a Model Binder into your application, you’ll use the ModelBinders class and its Binders collection. Here is how you’ll integrate the previous binder:

C#
ModelBinders.Binders.Add(typeof(Course), new CourseModelBinder());

The perfect place to put this line of code is in the Application_Start event in the Global.asax file.

Summary

Model Binders are very useful and save us a lot of development time. They help us to transform HTTP request input into model instances automatically, and by that makes our life easier. When in need, you can create your own custom Model Binder and integrate it into your MVC applications.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here