Some properties decimal, and decimal?in my view a model as a data type labeled "Percent", together with other data annotations, such as:
[DataType("Percent")]
[Display(Name = "Percent of foo completed")]
[Range(0, 1)]
public decimal? FooPercent { get; set; }
I would like to give the user some flexibility in the way they enter data, i.e. with or without percent sign, intermediate spaces, etc. But I still want to use the behavior DefaultModelBinderto get all its functions, for example, checking RangeAttributeand adding the appropriate verification messages.
Is there a way to parse and change the value of the model and then pass it on? This is what I am trying, but getting an exception at runtime. (Ignore the actual parsing logic, this is not its final form. I am interested in the issue of replacing the model at the moment.)
public class PercentModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
if (bindingContext.ModelMetadata.DataTypeName == "Percent")
{
ValueProviderResult result =
bindingContext.ValueProvider.GetValue(
bindingContext.ModelName);
if (result != null)
{
string stringValue =
(string)result.ConvertTo(typeof(string));
decimal decimalValue;
if (!string.IsNullOrWhiteSpace(stringValue) &&
decimal.TryParse(
stringValue.TrimEnd(new char[] { '%', ' ' }),
out decimalValue))
{
decimalValue /= 100.0m;
bindingContext.Model = decimalValue;
}
}
}
return base.BindModel(controllerContext, bindingContext);
}
}
source
share