public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { DemoVector vector; if (bindingContext.ModelType != typeof(DemoVector)) { return(false); } var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (value == null) { return(false); } var text = value.RawValue as string; if (DemoVector.TryParse(text, out vector)) { bindingContext.Model = vector; return(true); } bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Cannot convert value to vector."); return(false); }
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { var source = value as string; DemoVector vector; return(DemoVector.TryParse(source, out vector) ? vector : base.ConvertFrom(context, culture, value)); }