Model binding

Model binding is the process of mapping the Model data coming from the View to the ViewModel parameter of the action method in the Controller.

Let us consider a simple form with a couple of form fields—Name and EmailID. On the submission of the form, these values would be mapped to the ViewModel object of the action method of the Controller. Model binding takes care of this mapping. The Model binder looks for a match in the form fields, query strings, and request parameters.

In the preceding example, any class with these properties would be picked up by ModelBinder without any issues.

As the following Person class contains the Name and EmailID properties, the model binder would not complain about using this model for mapping the entered values in the form:

public class Person { 
  public string Name { get; set; } 
  public string EmailID { get; set; } 
} 

The following code snippet shows how to use the Person class in the action method:

public ActionResult Add(Person p) { 
  return View(); 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset