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?
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 ?
What Is Model Binding? ASP.NET MVC model binding allows mapping HTTP request data with a model. It is the procedure of creating . NET objects using the data sent by the browser in an HTTP request. Model binding is a well-designed bridge between the HTTP request and the C# action methods.
ValueProvider : ValueProvider in mvc is like parsed source of dictionary that help modelbinder to get appropriate value for its model. if you look at interface IValueProvider , It has two method. ContainPrefix and GetValue. ContainPrefix is help you to identify child model.
In fact, in ASP.NET MVC, there are three distinct types of model: the domain model, view model and input model.
The model binding system: Retrieves data from various sources such as route data, form fields, and query strings. Provides the data to controllers and Razor pages in method parameters and public properties.
ValueProviders are for getting data from different sources in the request
ModelBinders take the data from the ValueProviders to create an object. They don't directly take it from Route Data, or JSON or some specific, it's not their responsibility.
List of available ValueProviders in ASP.Net MVC 5, by priority order :
ChildActionValueProviderFactory
FormValueProviderFactory
JsonValueProviderFactory
RouteDataValueProviderFactory
QueryStringValueProviderFactory
HttpFileCollectionValueProviderFactor
List of available ValueProviders in ASP.Net Web API, by priority order :
QueryStringValueProviderFactory
RouteDataValueProviderFactory
Reference : Brad Wilson in Professional ASP.Net MVC 5, Wrox edition
Multiple ValueProviders
can cooperate to get data from different sources.
Priority matters if data is provided multiple times.
e.g. : id comes from querystring and RouteData - RouteData wins over querystring
All the best
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With