tags:

views:

145

answers:

1

I'm trying to create some HtmlHelper extensions and ran into a bit of a roadblock trying to get my extension methods to use attempted values defined by the ViewData.ModelState. The HtmlHelper.GetModelAttemptedValue() method is marked internal and isn't available to my extension methods. Is there a simple alternative in MVC?

+2  A: 

I'm not sure what you're going for here. Can't you just use the ViewData.ModelState available to the HtmlHelper and invoke TryGetValue on it yourself? I understand that it not DRY, but it seems easier than trying to invoke the three-line method in the helper via reflection.

public static string MyHelper( this HtmlHelper helper, string modelKey)
{
     ModelState modelState;
     if (helper.ViewData.ModelState.TryGetValue( modelKey, out modelState))
     {
        string attemptedValue = modelState.AttemptedValue;
     }
}
tvanfosson
Yeah, that's what I ended up doing. Thanks!
spoon16