Upgrading to ASP.NET MVC 2 - formCollection.ToValueProvider ()

I get the following error:

Cannot implicitly convert IValueProvider type to IDictionary

When I try to run the code below:

IDictionary<string, ValueProviderResult> valueProvider = formValues.ToValueProvider(); foreach (string k in formValues.Keys) { ModelState.SetModelValue(k, valueProvider[k]); } 

Can anyone help?

+4
source share
2 answers

Since I am using .NET 4.0, I had to do this:

 IValueProvider valueProvider = formValues.ToValueProvider(); foreach (string k in formValues.Keys) { ModelState.SetModelValue(k, valueProvider.GetValue(k)); } 

I want to say thanks to @abatishchev for pointing out the differences.

+1
source

You do a second, extra search in the dictionary:

 foreach (KeyValuePair<string, ValueProviderResult> pair in formValues) { ModelState.SetModelValue(pair.Key, pair.Value); } 

Very strange!

in 3.5 ToValueProvider() returns an IDictionary<string, ValueProviderResult>

and

but in 4.0 - returns IValueProvider

0
source

All Articles