Model Binding:-
This article explain about the Model binding in MVC application, This is great features in MVC application which helps developers to write only small set of code to achieve the functionality.

Before understanding about the Model Binding, let see how we normally retrieve the user input data. The following example shows how we retrieve data from request object.

 public ActionResult Edit()
   {
        string id = Request["Id"];
        string name = Request["Name"];
        string designation = Request["Designation"];
        int salary = Convert.ToInt32(Request["Salary"]);
        var emp = new Employee() { Id = id, Name = name,Designation = designation, Salary = salary };
   }


In this controller code, We need to retrieve all user input data from “Request” object, convert the data type and assign to properties, validate the user input data.

ASP.Net MVC provides the ‘Model Binding’ to simplify this process. The framework retrieves the data, maps data to object properties, validates the data and passes the model object to the controller as parameter.

  

[HttpPost]
 public ActionResult Edit(Employee employee)
 {
     if (ModelState.IsValid)
     {
         db.Entry(employee).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
            return View(employee);
 }
            


View:-

  

<fieldset>
        
        <legend>Employee</legend>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            
            @Html.LabelFor(model => model.Designation)
        </div>
        <div class="editor-field">
            
            @Html.EditorFor(model => model.Designation)
            @Html.ValidationMessageFor(model => model.Designation)
        </div>

        <div class="editor-label">
            
            @Html.LabelFor(model => model.Salary)
        </div>
        <div class="editor-field">
            
            @Html.EditorFor(model => model.Salary)
            @Html.ValidationMessageFor(model => model.Salary)
        </div>

        <p>
            
            <input type="submit" value="Save" />
        </p>
    </fieldset>




“ModelState.IsValid”:- It is used to validate the model binding object passed to the controller as parameter. “IsValid” property will return true if the data is valid.

Default Model Binder:- By default Asp.Net MVC provides the default binder which can bind the most of the data types like primitive type, simple objects, collection and complex object

Binding to Primitive Values:-

  public ActionResult Edit(string id, 
                string name, int salary, string designation)
        {
            return View();
        }


Binding to Collection:-

 public ActionResult Edit(int[] id)
        {
            return View();
        }


Binding to Simple Objects:-

 public ActionResult Edit(Employee employee)
        {
            return View();
        }


Binding to Collection of Objects:-

  public ActionResult Edit(IList<Employee> employee)
        {
            return View();
        }


Custom Model Binder:- ASP.Net MVC provides an option to create your own Model binder if you don’t want to use the default model binder. Custom model binder can be created by implementing the “IModelBinder” interface

Example:-

 using System.Web.Http.ModelBinding;

namespace ModelBindingExample
{
    public class CustomModelBinder:IModelBinder
    {
        public bool BindModel(System.Web.Http.
                        Controllers.HttpActionContext actionContext
                            , ModelBindingContext bindingContext)
        {
            throw new NotImplementedException();

        }
    }
}