Click here to Skip to main content
15,881,380 members
Articles / Web Development / ASP.NET
Tip/Trick

MVC Model Binder.

Rate me:
Please Sign up or sign in to vote.
4.52/5 (16 votes)
20 Oct 2014CPOL2 min read 30.7K   10   11
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)


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionWould you mind adding more complex examples? Pin
MCMickJ1923-Oct-14 6:01
MCMickJ1923-Oct-14 6:01 
GeneralRe: Would you mind adding more complex examples? Pin
IpsitaMishra23-Oct-14 7:56
IpsitaMishra23-Oct-14 7:56 
GeneralMy vote of 1 Pin
Member 1090330722-Oct-14 8:57
Member 1090330722-Oct-14 8:57 
QuestionCongratulations Pin
Gabriel José Hernández22-Oct-14 5:41
Gabriel José Hernández22-Oct-14 5:41 
AnswerRe: Congratulations Pin
IpsitaMishra22-Oct-14 6:18
IpsitaMishra22-Oct-14 6:18 
GeneralMy vote of 5 Pin
Carsten V2.021-Oct-14 2:28
Carsten V2.021-Oct-14 2:28 
GeneralRe: My vote of 5 Pin
IpsitaMishra21-Oct-14 2:34
IpsitaMishra21-Oct-14 2:34 
QuestionGood Simple Article Pin
tarzanbappa20-Oct-14 23:47
tarzanbappa20-Oct-14 23:47 
AnswerRe: Good Simple Article Pin
IpsitaMishra20-Oct-14 23:51
IpsitaMishra20-Oct-14 23:51 
AnswerRe: Good Simple Article Pin
aarif moh shaikh21-Oct-14 0:04
professionalaarif moh shaikh21-Oct-14 0:04 
GeneralRe: Good Simple Article Pin
IpsitaMishra21-Oct-14 0:08
IpsitaMishra21-Oct-14 0:08 

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.