views:

29

answers:

1

I am converting an ASP.NET MVC application to ASP.NET MVC 2, and I get the following error:

Cannot apply indexing with [] to an expression of type 'System.Web.Mvc.IValueProvider'

Here is the code:

public static void AddRuleViolation(this ModelStateDictionary modelState,
                                    RuleViolation error, 
                                    FormCollection collection)
{
    modelState.AddModelError(error.PropertyName, error.ErrorMessage);
    modelState.SetModelValue(error.PropertyName, 
                             collection.ToValueProvider()[error.PropertyName]);
}

How can this be remedied?

A: 

The implementation changed between ASP.NET MVC 1 and 2 for IValueProvider.

Try using the GetValue() method instead of referencing it by index.

collection.ToValueProvider().GetValue(error.PropertyName)
p.campbell