Click here to Skip to main content
12,692,778 members (34,652 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked

ASP.NET MVC Model Binders

, 20 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
What Model Binders are in ASP.NET MVC and how you can use them in your MVC applications.


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:

public ActionResult Edit(Customer customer)
    if (ModelState.IsValid)
        var mycustomer = _customerdb.GetCustomer(customer.Id);

        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:

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:

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; }


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

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


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:

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.


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...


Comments and Discussions

QuestionReg: HttpPost Pin
Member 968331813-Sep-15 22:15
memberMember 968331813-Sep-15 22:15 
Questioncan i get source code for this article? Pin
NithyaGopu3-Apr-14 20:32
memberNithyaGopu3-Apr-14 20:32 
Questioncan i get source code for this article? Pin
NithyaGopu3-Apr-14 20:32
memberNithyaGopu3-Apr-14 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 20 Jul 2011
Article Copyright 2011 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid