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. 最后,在需要验证的属性

相关内容

热门资讯

揭秘一下开挂技巧!掌上约牌多功... 揭秘一下开挂技巧!掌上约牌多功能开挂技巧,掌上约牌一贯真的是有开挂技巧1、掌上约牌模拟器是什么优化,...
技术分享外挂技巧!响水麻将多功... 技术分享外挂技巧!响水麻将多功能外挂技巧,响水麻将真是真的是有外挂技巧在进入响水麻将软件靠谱后,参与...
推荐几款新版作弊器技巧!响水麻... 推荐几款新版作弊器技巧!响水麻将多功能作弊器技巧,响水麻将都是是有作弊器技巧所有人都在同一条线上,像...
重要通知辅助app!乐通多功能... 重要通知辅助app!乐通多功能辅助app,乐通一直真的是有辅助app1、首先打开乐通辅助器下载最新版...
新手必备辅助器插件!朱雀吴江三... 新手必备辅助器插件!朱雀吴江三缺一多功能辅助器插件,朱雀吴江三缺一切实是有辅助器插件1、朱雀吴江三缺...
关于辅助器方法!澳门永利集团多... 关于辅助器方法!澳门永利集团多功能辅助器方法,澳门永利集团其实存在有辅助器方法1、在澳门永利集团插件...
一分钟快速了解辅助器app!乐... 一分钟快速了解辅助器app!乐夏多功能辅助器app,乐夏一贯存在有辅助器app1、起透看视 乐夏辅助...
免费测试版透视挂攻略!欢乐赣游... 免费测试版透视挂攻略!欢乐赣游GZ多功能透视挂攻略,欢乐赣游GZ竟然存在有透视挂攻略1.欢乐赣游GZ...
记者爆料辅助工具!panda多... 记者爆料辅助工具!panda多功能辅助工具,panda本来真的有辅助工具一、panda游戏安装教程牌...
避坑细节开挂工具!朋朋政和麻将... 您好,朋朋政和麻将这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...