ASP.Net MVC: What’s the Difference Between a Value Provider and Model Binder

Viewed 5417

I was reading a write up on What’s the Difference Between a Value Provider and Model Binder.

I understand that Model Binder basically get incoming data and build object. Suppose I am sending student data then model binder grab incoming student data when from post to server and build student object.

But still do not understand what is the job of Value Provider in MVC.

So please explain with easy sample that what kind of job done by Value Provider and what model binder does?

this is not clear what haacked.com is saying

The DefaultModelBinder will pull the Id value from the RouteData and the Age, FirstName, and LastName values from the JSON when building up the Person object. Afterwards, it’ll perform validation without having to know that the various values came from different sources.

When i am posting id,name,age etc then why model binder will pick id only from route data and rest of data from JSON. model binder should pick all value from RouteData or all value from JSON..............so why id only ?

How many different type of value provider exist in mvc ?

3 Answers

Model binder (for example, like the one binding to complex type) have the responsibility of going through the type graph (i.e recursively looking for bindable properties) and trying to find values for these properties. The binder gets these values from the value providers.

Just imagine if you were to write your own model binder similar to the complex type binder. It is non-trivial code. So one should carefully decide if they want to write a value provider or a model binder. You could think like "If I create a valueprovider, then I get all the recursive binding of properties for free".

Related