System.ComponentModel.DataAnnotations 使用介绍

RequiredAttribute:确保数据字段的值是必须的,不能为null或空。
KeyAttribute:标识一个属性作为实体的主键。
MaxLengthAttribute 和 MinLengthAttribute:定义字符串属性的最大和最小长度。
StringLengthAttribute:同时定义字符串属性的最大和最小长度。
RegularExpressionAttribute:用于验证属性值是否符合指定的正则表达式。
RangeAttribute:用于验证数值属性是否在指定的范围内。
DataTypeAttribute:用于指定属性的数据类型,如日期、时间、电子邮件等。
CompareAttribute:用于比较两个属性的值是否相等。
CustomValidationAttribute:用于实现自定义验证逻辑。

这些特性(Attribute)都是System.ComponentModel.DataAnnotations命名空间中提供的,它们用于在.NET应用程序中实现数据验证。下面是对这些特性的简单说明和用法示例:

RequiredAttribute

确保数据字段的值是必须的,不能为null或空。

csharp

public class MyModel  
{  
    [Required]  
    public string Name { get; set; }  
}

KeyAttribute

标识一个属性作为实体的主键。通常用于数据库上下文中。

csharp

public class MyModel  
{  
    [Key]  
    public int Id { get; set; }  
    public string Name { get; set; }  
}

MaxLengthAttribute 和 MinLengthAttribute

定义字符串属性的最大和最小长度。

csharp

public class MyModel  
{  
    [MaxLength(100)]  
    [MinLength(5)]  
    public string Description { get; set; }  
}

StringLengthAttribute

同时定义字符串属性的最大和最小长度。

csharp

public class MyModel  
{  
    [StringLength(100, MinimumLength = 5)]  
    public string ShortDescription { get; set; }  
}

RegularExpressionAttribute

用于验证属性值是否符合指定的正则表达式。

csharp

public class MyModel  
{  
    [RegularExpression(@"^\d{3}-\d{2}-\d{4}$")]  
    public string SocialSecurityNumber { get; set; }  
}

RangeAttribute

用于验证数值属性是否在指定的范围内。

csharp

public class MyModel  
{  
    [Range(typeof(int), "1", "100")]  
    public int Age { get; set; }  
}

DataTypeAttribute

用于指定属性的数据类型,如日期、时间、电子邮件等。

csharp

public class MyModel  
{  
    [DataType(DataType.EmailAddress)]  
    public string Email { get; set; }  
}

CompareAttribute

用于比较两个属性的值是否相等。注意:CompareAttribute 实际上不是 System.ComponentModel.DataAnnotations 命名空间的一部分,它可能属于其他库或框架,如 ASP.NET MVC。

假设它存在于某个库中,用法可能类似于:

csharp

public class MyModel  
{  
    public string Password { get; set; }  
  
    [Compare("Password")]  
    public string ConfirmPassword { get; set; }  
}

CustomValidationAttribute

用于实现自定义验证逻辑。这个特性通常与自定义的验证方法结合使用。

csharp

public class MyModel  
{  
    [CustomValidation(typeof(MyModel), "ValidateName")]  
    public string Name { get; set; }  
  
    public static ValidationResult ValidateName(string value, ValidationContext context)  
    {  
        if (value == "InvalidName")  
        {  
            return new ValidationResult("Invalid name provided.");  
        }  
        return ValidationResult.Success;  
    }  
}

请注意,CompareAttribute 并非标准 System.ComponentModel.DataAnnotations 命名空间的一部分,因此在使用时需要确保引用了正确的库或框架。此外,CustomValidationAttribute 需要与自定义的验证方法结合使用,该方法定义了验证逻辑,并返回 ValidationResult 对象来表示验证是否成功。

  • 18
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值