mohaidar
mohaidar

Reputation: 4276

how can we take the benefit of ToValueProvider in asp.net mvc?

my question is how can we take the advantage of ToValueProvider method in asp.net MVC and when to use it?

For example we could use the method

TryUpdateModel(d , collection.ToValueProvider());

but at the same time it is correct to use it without the second parameter, so what is the role that the ToValueProvider method plays at that context.

Upvotes: 1

Views: 841

Answers (1)

Valyok26
Valyok26

Reputation: 501

As you can see in sources FormCollection.ToValueProvider is just:

    public IValueProvider ToValueProvider()
    {
        return this;
    }

So I think It's just kept for compatibility with older versions of MVC. In MVC 2 it was only possible to use

TryUpdateModel(d , formCollection.ToValueProvider());

not

TryUpdateModel(d , formCollection);

In MVC 2 FormCollection didn't implement IValueProvider and ToValueProvider method returned IDictionary<string, ValueProviderResult> that is the type of the second parameter of Controller.TryUpdateModel in MVC 2. You can see it here and here.

UpdateModel with only one parameter searches for values in four places: form data, route data, the query string, and uploaded files. By using overloaded version with two parameters you can specify one of these places (form data for example) or use your own object implementing IValueProvider (this page on www.asp.net has example).

Upvotes: 2

Related Questions