ASP.NETMVC:基于两个字段长度的DataAnnotation自定义验证方法
创始人
2024-11-12 05:30:16
0
  1. 首先,在需要自定义验证的Model中,添加需要验证的两个属性。

public class MyModel { public string Field1 { get; set; }

public string Field2 { get; set; }

}

  1. 接着,定义一个新的自定义ValidationAttribute类,命名为StringLengthComparisonAttribute,并继承ValidationAttribute类。

public class StringLengthComparisonAttribute : ValidationAttribute { private readonly string _dependentProperty; private readonly ComparisonOperator _comparisonOperator; private readonly int _dependentPropertyValue;

public StringLengthComparisonAttribute(string dependentProperty, ComparisonOperator comparisonOperator, int dependentPropertyValue)
{
    _dependentProperty = dependentProperty;
    _comparisonOperator = comparisonOperator;
    _dependentPropertyValue = dependentPropertyValue;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    var dependentProperty = validationContext.ObjectType.GetProperty(_dependentProperty);
    if (dependentProperty == null)
    {
        return new ValidationResult($"Unknown property {_dependentProperty}");
    }

    var dependentPropertyValue = dependentProperty.GetValue(validationContext.ObjectInstance, null);

    if (!int.TryParse(dependentPropertyValue.ToString(), out int intDependentPropertyValue))
    {
        return new ValidationResult($"Property {_dependentProperty} is not of type int");
    }

    if (value == null)
    {
        return ValidationResult.Success;
    }

    var valueString = value.ToString();

    var dependentPropertyValueString = dependentPropertyValue.ToString();
    
    if(_comparisonOperator == ComparisonOperator.GreaterThan) 
    {
        if (valueString.Length > intDependentPropertyValue)
        {
            return new ValidationResult($"The length of {validationContext.DisplayName} must be greater than the length of {_dependentProperty}");
        }
    } 
    else if(_comparisonOperator == ComparisonOperator.LessThan) 
    {
        if (valueString.Length < intDependentPropertyValue)
        {
            return new ValidationResult($"The length of {validationContext.DisplayName} must be less than the length of {_dependentProperty}");
        }
    }
    
    return ValidationResult.Success;
}

}

在StringComparisonOperator枚举中定义比较运算符。

public enum ComparisonOperator { LessThan, GreaterThan }

  1. 最后,在需要验证的属性

相关内容

热门资讯

红龙扑克辅助工具!红龙扑克好友... 红龙扑克辅助工具!红龙扑克好友桌,(红龙扑克)果然有挂(详细辅助器教程);科技安装教程;136704...
微扑克辅助挂!wpk微扑克外挂... 微扑克辅助挂!wpk微扑克外挂事件,(微扑克规律)原来存在有挂(详细ai辅助器苹果版教程);超受欢迎...
wepower辅助器!wepo... 1、wepower辅助器!wepower透视辅助(wepoke app)好像真的是有挂(详细有挂教程...
红龙扑克辅助挂!红龙扑克辅助工... 红龙扑克辅助挂!红龙扑克辅助工具,(红龙扑克)总是真的有挂(详细辅助器教程),亲,有的,ai轻松简单...
WePoKe透明挂!wepok... WePoKe透明挂!wepoke用模拟器有什么用(Wepoke透明)真是有挂(详细真的有挂教程)关于...
aapoker透视辅助!aa扑... aapoker透视辅助新手教程相关信息汇总(需添加指定薇757446909获取下载链接);aapok...
德扑开发!德扑之星真的有辅吗,... 德扑开发!德扑之星真的有辅吗,德扑之星专业确实真的有挂(详细数据教程);亲真的是有正版授权,小编(透...
wepoke真的有挂!wepo... wepoke真的有挂新手教程相关信息汇总(需添加指定薇757446909获取下载链接);wepoke...
德扑之星发牌!德扑之星的机制,... 德扑之星发牌!德扑之星的机制,德扑之星ai软件好像真的有挂(详细线上教程)1、完成的残局,帮助玩家取...
红龙扑克辅助器!红龙扑克有挂么... 红龙扑克辅助器!红龙扑克有挂么,(红龙扑克)竟然有挂(详细辅助教程)平台为新手玩家设计了多种软件透明...