Not able to access GetModelStateValue in custom control in asp.net mvc2
Asked Answered
S

1

3

I am trying to write a custom control for text box control where in would like to generate the control based on some input attributes , I am not able to access GetModelStateValue in my custom control.

How do i do this ?

Stooge answered 6/8, 2011 at 13:33 Comment(1)
What have you tried so far? Please show your code. What doesn't work with your code and what you are trying to achieve? What is your question?Milky
S
26

You won't be able to access it as that method is marked as an internal method. The best you can do is just duplicate the MVC source code and place that method somewhere where you can access it. You can use the following. Note you will need to pass in the htmlHelper object to access the ViewData.

static object GetModelStateValue(HtmlHelper htmlHelper, string key, Type destinationType)
{
    ModelState modelState;
    if (htmlHelper.ViewData.ModelState.TryGetValue(key, out modelState))
    {
        if (modelState.Value != null)
        {
            return modelState.Value.ConvertTo(destinationType, null /* culture */);
        }
    }
    return null;
}
Steward answered 14/10, 2011 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.