Cannot apply indexing with [] to an expression of type 'System.Web.Mvc.IValueProvider'
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?
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)
精彩评论