Model Binding Architecture in ASP.NET MVC

Model Binding:

Model binding is the process of binding values posted over an HTTP request to the parameters used by the controller’s methods.

Model Binding Architecture:

The model-binding logic is encapsulated in a specific model-binder class. The binder works under the control of the action invoker and helps to figure out the parameters to pass to the selected controller method. In ASP.NET MVC, there are three distinct models:
i. Input Model
ii. Domain Model
iii. The View Model

The former describes the data you work with within the middle tier and is expected to provide a faithful representation of the entities and relationships that populate the business domain. These entities are typically persisted by the data-access layer and consumed by services that implement business processes. This domain model pushes a vision of data that is, in general, distinct and likely different from the vision of data you find in the presentation layer. The view model just describes the data that is being worked on in the presentation layer.

Model Binding Architecture in ASP.NET MVC

Input Model: It provides the representation of the data being posted to the controller.
Domain Model: It is the representation of the domain-specific entities operating in the middle tier.
View Model: It provides the representation of the data being worked on in the view.

Implementing a Model Binder:

The IModelBinder interface is defined as follows:

public interface IModelBinder
{
Object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext);
}
public class MyComplexTypeModelBinder : IModelBinder
{
public Object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException("bindingContext");
var obj = new MyComplexType();
obj.SomeProperty = FromPostedData<string>(bindingContext, "SomeProperty");
return obj;
}
private T FromPostedData<T>(ModelBindingContext context, String key)
{
ValueProviderResult result;
context.ValueProvider.TryGetValue(key, out result);
context.ModelState.SetModelValue(key, result);
return (T) result.ConvertTo(typeof(T));
}