Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

MVC Model Binder.

4.52/5 (16 votes)
20 Oct 2014CPOL2 min read 31.9K  
Here is a tip about how the Model binder work in MVC.

Introduction

Model Binding is a Bridge between the Htttp request and the MVC action method.Action methods which process the Http request takes parameter these parameters objects are created by the Model binding.Or in other word we can say that the method by which the URL segment converted into a method parameter is called a Model binding.

How It actually work.

Lets create a simple asp.net MVC project with a model class

Here in the demo application we have created one model class "Book"

C#
using System;
namespace MvcModels.Models {

public class Book
    {
        public int ID { get; set; }
        public string BookName { get; set; }
        public string AuthorName { get; set; }
        public string ISBN { get; set; }

    }


The BookController look like this

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcModels.Models;

 public class BookController : Controller
    {
public ActionResult Details(int id) {
Person bookItem = BookData.Where(p => p.ID == id).First();
return View(bookItem);
}
}


We have Create the correspinding details veiw /Views/Home/Details.cshtml and it look like this

HTML
@model KnowMoreMVC4.Models.Book

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>Book</legend>
    
    <table>
        <tr><td>BookName:</td><td>@Html.DisplayFor(model => model.BookName)</td></tr>
        <tr><td>AuthorName:</td><td>@Html.DisplayFor(model => model.AuthorName)</td></tr>
        <tr><td>ISBN:</td><td>@Html.DisplayFor(model => model.ISBN)</td></tr>
    </table>
 
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

 

Running the application will look as follows

Image 1


The Url we are providing in the address bar is as follows /Book/Details/1

The MVC Routing system translate the URL as Book is the Controller Details ia the action and 1 is the Parameter to server the request.

C#
...
public ActionResult Details(int id) {
...

The process by which the URL segment was converted into the int method argument is an example
of model binding

Default ModelBinder:-

Action invoker looks for the build in Model binder to bind the type.By default the model binder Find in 4 locations in the following orders

1.Request.Form["id"]:-Values provided by the user in HTML form elements.
2.RouteData.Values["id"]:-The values obtained using the application routes.
3.Request.QueryString["id"]:-Data included in the query string portion of the request URL.
4.Request.Files["Id"]:-Files that have been uploaded as part of the request.

The search is stopped as soon as a value in found.In the above eaxmaple first the form data is search but it does not found anything Then it will search in RoutData and it find a routing variable with rigth name.Once it finds the right name  it will not search anywhere here it will not go to  search the querystring and uploaded files.

Here it is very important that the parameter in the action method should always match the dataproperty we are looking for.

 

License

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