5

在我看来,模型中的一些decimaldecimal?属性标记为“百分比”数据类型,与其他数据一起注释,例如前操纵模型值:它传递给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); 
    } 
} 

回答

5

没关系,这是在哪里验证发生在一个根本性的误解MVC周期。在花了一段时间在MVC源代码之后,我看到它是如何工作的。

如果它是有帮助的人,这里是什么工作对我来说:

[DataType("Percent")] 
[Display(Name = "Percent of foo completed")] 
[Range(0.0d, 1.0d, ErrorMessage="The field {0} must be between {1:P0} and {2:P0}.")] 
public decimal? FooPercent { get; set; } 

而在粘合剂,你可以返回值:

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)) 
       { 
        return decimalValue/100.0m; 
       } 
      } 
     } 

     return base.BindModel(controllerContext, bindingContext); 
    } 
} 
+0

对我来说,定制粘合剂从未执行过。你还在做什么? – 2018-03-06 10:30:14