当对CheckBox使用ValidationAttribute验证时,系统没有针对bool做True的验证。需要自己写一个。代码如下:
public class MustBeTrueAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value is bool && (bool)value == true) return ValidationResult.Success; return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); } }
时间: 2024-11-05 13:28:56