Is there a simple alternative to GetModelAttemptedValue in HtmlHelpers?

I am trying to create some HtmlHelper extensions and ran into a little obstacle trying to get my extension methods to use the attempts defined in ViewData.ModelState. The method is HtmlHelper.GetModelAttemptedValue()

marked as internal and is not available to my extension methods. Is there a simple alternative in MVC?

+1


source to share


1 answer


I'm not sure what you are going here for. Can't you just use the ViewData.ModelState for the HtmlHelper and call TryGetValue yourself? I realize this is not DRY, but it seems easier than trying to call a 3-line method in a 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;
     }
}

      

+2


source







All Articles