【发布时间】:2017-09-23 03:22:09
【问题描述】:
在 asp.net mvc 应用程序中,我创建了自定义模型绑定器和 IModelBinderProvider 以在货币以千位格式发布到服务器时处理货币,例如 $123,456.00
只有当模型的属性应用了某些属性时,我才想调用自定义模型绑定器。下面是我的代码
public interface IScrubberAttribute
{
object Scrub(string modelValue, out bool success);
}
public class CurrencyScrubberAttribute : Attribute, IScrubberAttribute
{
public object Scrub(string modelValue, out bool success)
{
// do something
}
}
public class ScrubbingModelBinder : DefaultModelBinder
{
IScrubberAttribute _attribute;
public ScrubbingModelBinder(Type type, IScrubberAttribute attribute)
{
_attribute = attribute as IScrubberAttribute;
}
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// do something
}
}
public class ScrubbingModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(Type modelType)
{
if (modelType == typeof(decimal) || modelType == typeof(decimal?))
{
//??? ISSUE: the line below always returns null.
var attribute = modelType.GetCustomAttributes(typeof(CurrencyScrubberAttribute), false).FirstOrDefault();
if (attribute != null)
{
return new ScrubbingModelBinder(modelType, attribute as IScrubberAttribute);
}
}
return null;
}
}
我的模型
public class MyModel
{
[CurrencyScrubber]
public decimal? MyValue { get; set; }
}
我在应用程序启动时注册了ScrubbingModelBinderProvider,所以它会被调用。
问题
在ScrubbingModelBinderProvider 中,我试图查找该属性是否应用了[CurrencyScrubber] 属性;如果是则只调用ScrubbingModelBinder。
但是modelType.GetCustomAttributes() 方法无法找到或返回CurrancyScrubber 属性。当我在调试模式下快速观看时,modelType.GetCustomAttributes() 方法返回 3 个属性,但没有一个属性属于 CurrancyScrubber
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-5 model-binding