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:
在我的应用程序中,我试图从日期和时间字段中分割日期和时间,以便我可以在日期上放置一个jQuery日期选择器。我找到了Hanselman用于拆分DateTime的代码,但是我在bindingContext.ValueProvider.TryGetValue(modelName,out valueResult);上遇到了编译错误。我得到的错误是:
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
错误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项目
What am I missing something? I created a new class and put his code in a Helpers folder in my project.
我错过了什么?我创建了一个新类,并将他的代码放在我项目的Helpers文件夹中。
2 个解决方案
#1
14
TryGetValue()
is not a member of System.Web.Mvc.IValueProvider
.
I suspect he has a custom extension which looks something like:
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()
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.
TryGetValue()不是扩展方法,而是IDictionary
#2
5
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:
前几天我试图按照汉塞尔曼的例子来解决这个问题。这不是MVC2的例子。 TryGetValue不再起作用和/或不再需要。试试这个链接:
http://forums.asp.net/p/1529895/3706154.aspx
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.
我创建了一个来自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;
}
}
#1
14
TryGetValue()
is not a member of System.Web.Mvc.IValueProvider
.
I suspect he has a custom extension which looks something like:
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()
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.
TryGetValue()不是扩展方法,而是IDictionary
#2
5
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:
前几天我试图按照汉塞尔曼的例子来解决这个问题。这不是MVC2的例子。 TryGetValue不再起作用和/或不再需要。试试这个链接:
http://forums.asp.net/p/1529895/3706154.aspx
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.
我创建了一个来自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;
}
}