无法访问GetModelStateValue自定义控制在asp.net MVC2 [英] Not able to access GetModelStateValue in custom control in asp.net mvc2

查看:205
本文介绍了无法访问GetModelStateValue自定义控制在asp.net MVC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个文本框控件的自定义控件其中想基于一些输入属性生成控制,我不能够访问GetModelStateValue在我的自定义控件。

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.

我如何做到这一点?

推荐答案

您不会因为该方法被标记为内部的方法可以访问它。你能做的最好是刚刚复制的MVC源$ C ​​$ C,并将该方法的地方在那里,你可以访问它。您可以使用以下。注意,您需要在对象的HtmlHelper访问ViewData的传递。

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;
}

这篇关于无法访问GetModelStateValue自定义控制在asp.net MVC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆