public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var propertyName = bindingContext.ModelName; var rawValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).RawValue; var queryArgument = rawValue is string?(string)rawValue : ((string[])rawValue)[0]; var comparisonPredicate = ComparisonPredicate.Parse(queryArgument); var filter = (IPropertyFilterInitializer)Activator.CreateInstance(bindingContext.ModelType); filter.Initialize(propertyName, comparisonPredicate); return(filter); }
void IPropertyFilterInitializer.Initialize(string propertyName, ComparisonPredicate predicate) { Name = propertyName; Operator = predicate.Operator; RawValue = predicate.RawValue; if (Operator != ComparisonOperator.Any) { // use converter if exists to give a chance to types (like enum) to provide a custom conversion // TODO should use a converter specified property-wise, see http://stackoverflow.com/questions/458935/extend-a-typeconverter var converter = TypeDescriptor.GetConverter(typeof(T)); Value = (T)(converter != null && converter.CanConvertFrom(typeof(string)) ? converter.ConvertFrom(predicate.Value) : Convert.ChangeType(predicate.Value, typeof(T))); } }