开发者

Getting the validation metadata for built in validators

How do you get the validation metada开发者_开发知识库ta for the built in validation attributes?

I'm trying to implement a word count/character count on a form giving the remaining number allowed. As I am implementing the word count validation and metadata myself (adding it into AdditionalValues) I can access this fine, but having spent ages looking I can't find where to get maximumLength from the StringLength attribute.


There is no built in way to get the maxLength from the StringLength attribute.

You have to find it manually using reflection, something like:

foreach( var property in model.GetType() )
{
     var stringLengthAttr = property.GetCustomAttributes(typeof(StringLengthAttribute), false).FirstOrDefault() as StringLengthAttribute;

     if( stringLengthAttr != null )
         return stringLengthAttr.MaximumLength;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜