Click here to Skip to main content
15,867,939 members
Articles / Web Development / ASP.NET

ASP.NET MVC Model Binders

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
20 Jul 2011CPOL2 min read 24.5K   7   3
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.

This article was originally posted at http://feeds.feedburner.com/GilFinkBlog

License

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


Written By
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: http://www.gilfink.net

Comments and Discussions

 
QuestionReg: HttpPost Pin
Member 968331813-Sep-15 21:15
Member 968331813-Sep-15 21:15 
Questioncan i get source code for this article? Pin
NithyaGopu3-Apr-14 19:32
NithyaGopu3-Apr-14 19:32 
Questioncan i get source code for this article? Pin
NithyaGopu3-Apr-14 19:32
NithyaGopu3-Apr-14 19: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.