Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net-mvc

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 ?

like image 975
Thomas Avatar asked Mar 04 '16 14:03

Thomas


People also ask

What is model binder 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.

What is value provider MVC?

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.

What are the three models of MVC?

In fact, in ASP.NET MVC, there are three distinct types of model: the domain model, view model and input model.

What is the purpose of model binding?

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.


1 Answers

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 :

    1. ChildActionValueProviderFactory
    2. FormValueProviderFactory
    3. JsonValueProviderFactory
    4. RouteDataValueProviderFactory
    5. QueryStringValueProviderFactory
    6. HttpFileCollectionValueProviderFactor
  • List of available ValueProviders in ASP.Net Web API, by priority order :

    1. QueryStringValueProviderFactory
    2. 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

like image 92
Emmanuel DURIN Avatar answered Sep 29 '22 07:09

Emmanuel DURIN