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?
views:
145answers:
1
+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
2008-11-17 23:01:33
Yeah, that's what I ended up doing. Thanks!
spoon16
2008-11-17 23:10:26