通过


RangeAttribute 类

定义

指定数据字段值的数值范围约束。

public ref class RangeAttribute : System::ComponentModel::DataAnnotations::ValidationAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)]
public class RangeAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public class RangeAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)>]
type RangeAttribute = class
    inherit ValidationAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)>]
type RangeAttribute = class
    inherit ValidationAttribute
Public Class RangeAttribute
Inherits ValidationAttribute
继承
属性

示例

以下示例演示如何使用 RangeAttribute 数据字段自定义格式设置。 此示例执行以下步骤:

  • 实现元数据分部类和关联的元数据类。

  • 在关联的元数据类中 RangeAttribute ,它应用属性以获取以下结果:

    • 将属性应用于整数类型的数据字段。

    • 将属性应用于整数数据字段并定义自定义验证错误消息。

    • 将属性应用于 DateTime 数据字段并定义自定义验证错误消息。

using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

[MetadataType(typeof(ProductMetaData))]
public partial class Product
{
}

public class ProductMetaData
{
    
    [Range(10, 1000, 
        ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    public object Weight;

    [Range(300, 3000)]
    public object ListPrice;

    [Range(typeof(DateTime), "1/2/2004", "3/4/2004",
        ErrorMessage = "Value for {0} must be between {1} and {2}")]
    public object SellEndDate;
}
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel

<MetadataType(GetType(ProductMetaData))> _
Partial Public Class Product

End Class


Public Class ProductMetaData
    
    <Range(10, 1000, _
           ErrorMessage:="Value for {0} must be between {1} and {2}.")> _
    Public Weight As Object

    <Range(300, 3000)> _
    Public ListPrice As Object

    <Range(GetType(DateTime), "1/2/2004", "3/4/2004", _
           ErrorMessage:="Value for {0} must be between {1} and {2}")> _
    Public SellEndDate As Object

End Class

若要编译示例,需要满足以下条件:

  • Microsoft Visual Studio 2008 Service Pack 1 或 Visual Web Developer 2008 Express Edition SP1。

  • 数据驱动的网站。 这样,便可以为数据库和包含要自定义的数据字段的类创建数据上下文。 有关详细信息,请参阅 Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding

注解

将此属性应用于数据字段时,必须遵循有关如何使用验证属性的准则。

构造函数

名称 说明
RangeAttribute(Double, Double)

使用指定的最小值和最大值初始化类的新实例 RangeAttribute

RangeAttribute(Int32, Int32)

使用指定的最小值和最大值初始化类的新实例 RangeAttribute

RangeAttribute(Type, String, String)

使用指定的最小值和最大值和特定类型初始化类的新实例 RangeAttribute

属性

名称 说明
ConvertValueInInvariantCulture

获取或设置一个值,该值确定是否将type构造函数的参数RangeAttribute(Type, String, String)验证为OperandType所验证的值的任何转换都使用固定区域性或当前区域性。

ErrorMessage

获取或设置一条错误消息,以在验证失败时与验证控件相关联。

(继承自 ValidationAttribute)
ErrorMessageResourceName

获取或设置要使用的错误消息资源名称,以便在验证失败时查找 ErrorMessageResourceType 属性值。

(继承自 ValidationAttribute)
ErrorMessageResourceType

获取或设置在验证失败时用于错误消息查找的资源类型。

(继承自 ValidationAttribute)
ErrorMessageString

获取本地化的验证错误消息。

(继承自 ValidationAttribute)
Maximum

获取允许的最大字段值。

MaximumIsExclusive

指定是否应对等于 Maximum的值进行验证。

Minimum

获取允许的最小字段值。

MinimumIsExclusive

指定是否应对等于 Minimum的值进行验证。

OperandType

获取必须验证其值的数据字段的类型。

ParseLimitsInInvariantCulture

获取或设置一个值,该值确定是否使用固定区域性而不是当前区域性来分析其字符串值MinimumMaximum

RequiresValidationContext

获取一个值,该值指示属性是否需要验证上下文。

(继承自 ValidationAttribute)
TypeId

在派生类中实现时,获取此 Attribute的唯一标识符。

(继承自 Attribute)

方法

名称 说明
Equals(Object)

返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
FormatErrorMessage(String)

设置范围验证失败时显示的错误消息的格式。

GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
GetValidationResult(Object, ValidationContext)

检查指定的值是否对当前验证属性有效。

(继承自 ValidationAttribute)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否为派生类的默认值。

(继承自 Attribute)
IsValid(Object, ValidationContext)

验证与当前验证属性相关的指定值。

(继承自 ValidationAttribute)
IsValid(Object)

检查数据字段的值是否在指定范围内。

Match(Object)

在派生类中重写时,返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
Validate(Object, String)

验证指定的对象。

(继承自 ValidationAttribute)
Validate(Object, ValidationContext)

验证指定的对象。

(继承自 ValidationAttribute)

显式接口实现

名称 说明
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,该信息可用于获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对对象公开的属性和方法的访问。

(继承自 Attribute)

适用于