ValueProvider 不包含 TryGetValue 的定义

发布于 2024-10-01 07:23:15 字数 683 浏览 4 评论 0原文

在我的应用程序中,我尝试从 DateTime 字段中拆分日期和时间,以便我可以在日期上放置 jQuery 日期选择器。我发现 Hanselman 的用于分割日期时间的代码,但是我在 上收到编译错误bindingContext.ValueProvider.TryGetValue(modelName, out valueResult);。我得到的错误是:

错误 3“System.Web.Mvc.IValueProvider”不包含“TryGetValue”的定义,并且找不到接受“System.Web.Mvc.IValueProvider”类型的第一个参数的扩展方法“TryGetValue”(您缺少 using 指令或程序集引用吗?) C:\Documents and Settings\xxx\My Documents\Visual Studio 2008\Projects\MyProject\Project\Helpers\DateAndTimeModelBinder.cs 83 42 Project

我缺少什么?我创建了一个新类并将其代码放在我项目的 Helpers 文件夹中。

In my application, I am trying to split the Date and Time from and DateTime field so I can put a jQuery date picker on the date. I found Hanselman's code for splitting the DateTime, however I get a compile error on bindingContext.ValueProvider.TryGetValue(modelName, out valueResult);. The error I get is:

Error 3 'System.Web.Mvc.IValueProvider' does not contain a definition for 'TryGetValue' and no extension method 'TryGetValue' accepting a first argument of type 'System.Web.Mvc.IValueProvider' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\xxx\My Documents\Visual Studio 2008\Projects\MyProject\Project\Helpers\DateAndTimeModelBinder.cs 83 42 Project

What am I missing something? I created a new class and put his code in a Helpers folder in my project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-10-08 07:23:15

TryGetValue() 不是 System.Web.Mvc.IValueProvider 的成员。 我怀疑他有一个自定义扩展,看起来像这样:

public static bool TryGetValue(this IValueProvider valueProvider, string key, out ValueProviderResult result) {
    try {
        result = valueProvider.GetValue(key);
        return true;
    }
    catch {
        result = null;
        return false;
    }
}

Update

TryGetValue() 不是扩展方法,而是它的方法类型 IDictionary。正如 @mootinator 所指出的,自 MVC1 以来,BindingContext.ValueProvider 的类型已发生变化。您可以忽略对 TryGetValue() 的调用,而是调用 GetValue() 并检查结果是否为 null。我不确定它是否会抛出异常,因为我还没有测试过,所以先尝试一下。

TryGetValue() is not a member of System.Web.Mvc.IValueProvider. I suspect he has a custom extension which looks something like:

public static bool TryGetValue(this IValueProvider valueProvider, string key, out ValueProviderResult result) {
    try {
        result = valueProvider.GetValue(key);
        return true;
    }
    catch {
        result = null;
        return false;
    }
}

Update

TryGetValue() is not an extension method, but rather it is a method on the type IDictionary<T,U>. The type of bindingContext.ValueProvider has changed since MVC1 as @mootinator indicated. It's possible you can just ignore the call to TryGetValue() and instead call GetValue() and check the result for null. I'm not sure if it will throw an exception as I haven't tested it, so try that first.

酷到爆炸 2024-10-08 07:23:15

前几天我在尝试效仿汉塞尔曼的例子时遇到了这个问题。这不是 MVC2 示例。 TryGetValue 不起作用和/或不再需要。试试这个链接:

http://forums.asp.net/p/1529895/3706154.aspx

我从 Hanselman 的 GetA 方法创建了一个 MVC2 扩展方法来替换,尽管我不确定它是否按预期工作,因为它没有解决我的独特问题,这实际上与日期没有任何关系或时间。

public static T? GetA<T>(this ModelBindingContext bindingContext, string key) where T : struct
        {
            T? valueResult = null;
            if (String.IsNullOrEmpty(key)) return null;
            //Try it with the prefix...
            try
            {
                valueResult = (T?)bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + key).ConvertTo(typeof (T));
            } catch (NullReferenceException){}
            //Didn't work? Try without the prefix if needed...
            if (valueResult == null && bindingContext.FallbackToEmptyPrefix == true)
            {
                try
                {
                    valueResult = (T?) bindingContext.ValueProvider.GetValue(key).ConvertTo(typeof (T));
                } catch (NullReferenceException){}
            }
            return valueResult;
        }
    }

I had this problem trying to follow Hanselman's example the other day. It's not an MVC2 example. TryGetValue doesn't work and/or isn't needed anymore. Try this link:

http://forums.asp.net/p/1529895/3706154.aspx

I created an MVC2 extension method from Hanselman's GetA method to replace, though I'm not sure whether it works as intended, since it didn't solve my unique problem, which didn't actually have anything to do with date or time.

public static T? GetA<T>(this ModelBindingContext bindingContext, string key) where T : struct
        {
            T? valueResult = null;
            if (String.IsNullOrEmpty(key)) return null;
            //Try it with the prefix...
            try
            {
                valueResult = (T?)bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + key).ConvertTo(typeof (T));
            } catch (NullReferenceException){}
            //Didn't work? Try without the prefix if needed...
            if (valueResult == null && bindingContext.FallbackToEmptyPrefix == true)
            {
                try
                {
                    valueResult = (T?) bindingContext.ValueProvider.GetValue(key).ConvertTo(typeof (T));
                } catch (NullReferenceException){}
            }
            return valueResult;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文