使用 MVC4 我需要ModelMetadata从自定义的ValidationAttribute. 这样做的原因是验证属性需要知道哪些其他属性附加到正在验证的特定属性。
具体来说,它会检查该财产是否有UppercaseAttribute附属物;如果是这种情况,那么它将执行不同的逻辑路径。如果可以ModelMetadata从 中获取ValidationAttribute,我将使用以下代码进行检查:
ModelMetadata.ContainerType
.GetProperty(ViewData.ModelMetadata.PropertyName)
.GetCustomAttributes(typeof(UppercaseAttribute), true)
首先,是否可以ModelMetadata从 custom中检索ValidationAttribute.
其次,上面的代码是检查属性是否附加了特定属性的最佳实践。