ValueProvider does not contain a definition for TryGetValue

前端 未结 2 844
青春惊慌失措
青春惊慌失措 2021-01-17 17:57

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

2条回答
  •  一个人的身影
    2021-01-17 18:45

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

提交回复
热议问题