views:

71

answers:

2

I am getting the following error:

Cannot implicitly convert type IValueProvider 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?

A: 

You do second, superfluous dictionary lookup:

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

Very strange!

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

and

but in 4.0 - it returns IValueProvider

abatishchev
when i add in your foreach loop i get the error Specified cast is not valid. for pair. what do i need to do there?
dean nolan
@dean: I updated my post. anyway try to cast - what result?
abatishchev
Hmm, maybe that explains it, I am using 4.0. I Will try casting to IValueProvider and see if that works. Thanks
dean nolan
@dean: have you any results with cast?
abatishchev
A: 

As 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.

dean nolan
It's rather better to delete this your answer and edit your original question
abatishchev
really? I thought it was best to have an answer for the question?
dean nolan
@dean: Sorry I just didn't understand that's your own answer to your question! Glad to hear you found the solution. Approve it now
abatishchev