Home > DeveloperSection > Forums > What is a MVC model binders?
Anupam Mishra

Total Post:135

Points:949
Posted on    February-18-2016 10:28 PM

 ASP.Net C#  ASP.Net  ASP.NET MVC  Mvc 
Ratings:


 1 Reply(s)
 304  View(s)
Rate this:
Hi,
I want to know about MVC model binders and how to implement it in mvc.Please can anyone give me a solution.
Thank you.


Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    February-18-2016 11:03 PM

Hi Anupam,
Here we explaining about Model binders:
Model binder maps HTML form elements to the model. It acts like a bridge between HTML UI and MVC model. Many times HTML UI names are different than the model property names.
 So in the binder we can write the mapping logic between the UI and the model.

Now Bydefault, we call DefaultModelBinder class that are internally implement  IModelBinder interface.
Here, a Student Model:
        public class Student
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int ID { get; set; }
            [Required]
            [StringLength(50,MinimumLength=1)]
            [Display(Name = "FirstName")]
            public string FirstName { get; set; }
            [Required]
            [StringLength(50, MinimumLength = 1)]
            [Display(Name = "LastName")]
            public string LastName { get; set; }
            [DataType(DataType.DateTime)]


Here is the view which will be mapped with the Student model.

 

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
       .......


Create one class and implement IModelBinder interface within it. In the below snippet, we have created “StudentModelBinder ” custom binding class and implemented IModelBinder interface. We have seen that IModelBinder  contains only one method called BindModel and we have implemented the logic to map view property with the model property.


public class StudentModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var request = controllerContext.HttpContext.Request;
            string first_name = request.Form.Get("FirstName");
            string last_name = request.Form.Get("LastName");
            return new Person { full_name = first_name + last_name };
        }
    }

Here, we are combining first_name and last_name to combine full name and returning Student object accordingly. Fine, now we will create the controller which will contain PostData() action and as parameter, it will take Student type object after binding in our custom StudentModelBinder class. Here is the simple implementation.


public class CustomodelbinderController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public void PostData([ModelBinder(typeof(StudentModelBinder))] Student student)
        {
        }
    }


Now, we have to register the custom binder class in MVC pipeline. Now we Just add the below line in global.asax file.


ModelBinders.Binders.Add(typeof(Student), new StudentModelBinder());]

 

Finally, we have setup everything to bind model using our custom class.


Modified On Feb-18-2016 11:05:14 PM

Don't want to miss updates? Please click the below button!

Follow MindStick