【问题标题】:Not able to access GetModelStateValue in custom control in asp.net mvc2无法在 asp.net mvc2 的自定义控件中访问 GetModelStateValue
【发布时间】:2011-10-15 03:03:24
【问题描述】:

我正在尝试为文本框控件编写一个自定义控件,其中想要基于某些输入属性生成控件,我无法在我的自定义控件中访问 GetModelStateValue。

我该怎么做?

【问题讨论】:

  • 到目前为止你尝试过什么?请出示您的代码。什么不适用于您的代码以及您想要实现的目标?你有什么问题?

标签: asp.net asp.net-mvc-2


【解决方案1】:

您将无法访问它,因为该方法被标记为内部方法。您能做的最好的事情就是复制 MVC 源代码并将该方法放在您可以访问它的地方。您可以使用以下内容。请注意,您需要传入 htmlHelper 对象才能访问 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多