Click here to Skip to main content
Click here to Skip to main content

ASP.NET MVC Model Binders

, 20 Jul 2011
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.

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:

[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:

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

    #endregion
}

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

  #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:

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gil Fink
Architect Sela Group
Israel Israel
Gil Fink is an expert in web development and Microsoft web and data platforms. He works as a senior consultant and architect at Sela Group. 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 a co-author of several Microsoft Official Courses and training kits and the founder of Front-End.IL meetup. You can read his publications at his website: http://www.gilfink.net
Follow on   Twitter   Google+

Comments and Discussions

 
Questioncan i get source code for this article? PinmemberNithyaGopu3-Apr-14 19:32 
Questioncan i get source code for this article? PinmemberNithyaGopu3-Apr-14 19:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 20 Jul 2011
Article Copyright 2011 by Gil Fink
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid