5
在我看来,模型中的一些decimal
和decimal?
属性标记为“百分比”数据类型,与其他数据一起注释,例如前操纵模型值:它传递给DefaultModelBinder.BindModel
[DataType("Percent")]
[Display(Name = "Percent of foo completed")]
[Range(0, 1)]
public decimal? FooPercent { get; set; }
我会例如允许用户在输入数据方面有一定的灵活性,即有或没有百分号,中间空格等。但我仍想使用DefaultModelBinder
行为来获得其所有功能,例如检查RangeAttribute
并添加适当的验证消息。
有没有办法解析并更改模型的值,然后将其传递?这是我正在尝试,但得到一个运行时异常。 (忽略实际的分析逻辑,这不是它的最终形式,我在这一点上的模型替换问题只是感兴趣。)
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;
// EXCEPTION : This property setter is obsolete,
// because its value is derived from
// ModelMetadata.Model now.
bindingContext.Model = decimalValue;
}
}
}
return base.BindModel(controllerContext, bindingContext);
}
}
对我来说,定制粘合剂从未执行过。你还在做什么? – 2018-03-06 10:30:14