How to check for a property attribute inside a custom model binder

非 Y 不嫁゛ 提交于 2019-12-04 05:09:45
Radim Köhler

During binding of the Model properties, we have access to property owner via:

bindingContext.ModelMetadata.ContainerType.

So the below snippet should give you variable hasAttribute set to true for FutureFecha property

var holderType = bindingContext.ModelMetadata.ContainerType;
if (holderType != null)
{
  var propertyType = holderType.GetProperty(bindingContext.ModelMetadata.PropertyName);
  var attributes = propertyType.GetCustomAttributes(true);
  var hasAttribute = attributes
    .Cast<Attribute>()
    .Any(a => a.GetType().IsEquivalentTo(typeof (FutureDateTime)));
  if(hasAttribute) ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!