Saturday, January 25, 2014

Model Binding in ASP.NET MVC

ASP.NET MVC Model Binding simplifies controller actions by automatically populating controller action parameters, taking care of property mapping and type conversion. 

Model binding consists of collecting values from the request and populating models with those values. These steps are accomplished by value providers and model binders. 


Value Providers

ASP.NET MVC includes value provider implementations that covers most common sources of request values such as Query String parameters, form fields and route data. You can even create your own custom value providers.

Value providers are registered in ValueProviderFactory. Following are some inherited value providers from ValueProviderFactory,
  • CookieValueProviderFactory
  • SessionValueProviderFactory
  • FormValueProviderFactory
  • JQueryFormValueProviderFactory
  • QueryStringValueProviderFactory


JSON Value Provider #
Enables MVC actions to send/receive JSON data and model bind JSON text to parameters of action method. This runs before model binding. JSON value provider serializes request data into a dictionary. Then this data is passed to the model binder.


Model Binders

ASP.NET MVC has default model binder DefaultModelBinder which maps browser request to a data object. It will go through each property in the target model and will check if it is a simple or a complex type. If it is a simple type, It'll try to convert it to a target type. If it is a complex type it will perform a recursive binding.


Resources

http://msdn.microsoft.com/en-us/magazine/hh781022.aspx
http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx/
http://www.codeproject.com/Tips/806415/Model-Binding-using-IModelBinder-and-DefaultModelB
http://stackoverflow.com/questions/8535802/when-to-use-imodelbinder-versus-defaultmodelbinder
http://chimera.labs.oreilly.com/books/1234000001708/ch13.html#_the_importance_of_models_in_asp_net_web_api

0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook