Как использовать asp.net mvc 3 пользовательский связыватель модели GetPropertyValue параметр propertybinder - PullRequest
1 голос
/ 16 декабря 2011

У меня есть пользовательский механизм связывания модели, где я проверяю, являются ли строковые свойства нулевыми, и заменяю их пустыми строками.

Я переопределяю метод BindProperty, но не уверен, как получить значение свойства из PropertyDescriptor

    GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, 
                     <What should I pass for the IModelBinder?>);

Вот код BindProperty

protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.PropertyType == typeof(string))
            {
                object s = GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, null);
                if (s == null)
                {
                    SetProperty(controllerContext, bindingContext, propertyDescriptor, "");
                }
            }
        }

1 Ответ

0 голосов
/ 16 декабря 2011

Я использую это разные варианты:

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

        private static DateTime? GetADateTime(ModelBindingContext bindingContext, string key)
        {
            if (String.IsNullOrEmpty(key)) return null;
            //Try it with the prefix...
            var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + key);
            //Didn't work? Try without the prefix if needed...
            if (valueResult == null && bindingContext.FallbackToEmptyPrefix)
            {
                valueResult = bindingContext.ValueProvider.GetValue(key);
            }
            if (valueResult == null)
            {
                return null;
            }
            return DateTime.Parse(valueResult.AttemptedValue, valueResult.Culture);
        }

        private static string GetValue(ModelBindingContext bindingContext, string key)
        {
            if (String.IsNullOrEmpty(key)) return null;
            //Try it with the prefix...
            var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + key);
            //Didn't work? Try without the prefix if needed...
            if (valueResult == null && bindingContext.FallbackToEmptyPrefix)
            {
                valueResult = bindingContext.ValueProvider.GetValue(key);
            }
            if (valueResult == null)
            {
                return null;
            }
            return valueResult.AttemptedValue;
        }

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...