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。

  • データ ドリブン Web サイト。 これにより、カスタマイズするデータ フィールドを含むデータベースとクラスのデータ コンテキストを作成できます。 詳細については、Walkthrough: Creating a New Dynamic Data Web Site using Scaffoldingを参照してください。

注釈

この属性をデータ フィールドに適用する場合は、検証属性の使用方法に関するガイドラインに従う必要があります。

コンストラクター

名前 説明
RangeAttribute(Double, Double)

指定した最小値と最大値を使用して、 RangeAttribute クラスの新しいインスタンスを初期化します。

RangeAttribute(Int32, Int32)

指定した最小値と最大値を使用して、 RangeAttribute クラスの新しいインスタンスを初期化します。

RangeAttribute(Type, String, String)

指定した最小値と最大値、および特定の型を使用して、 RangeAttribute クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
ConvertValueInInvariantCulture

RangeAttribute(Type, String, String) コンストラクターのtype パラメーターによって設定されたOperandTypeに検証される値の変換で、インバリアント カルチャまたは現在のカルチャを使用するかどうかを決定する値を取得または設定します。

ErrorMessage

検証が失敗した場合に検証コントロールに関連付けるエラー メッセージを取得または設定します。

(継承元 ValidationAttribute)
ErrorMessageResourceName

検証が失敗した場合に ErrorMessageResourceType プロパティ値を検索するために使用するエラー メッセージ リソース名を取得または設定します。

(継承元 ValidationAttribute)
ErrorMessageResourceType

検証が失敗した場合にエラー メッセージ検索に使用するリソースの種類を取得または設定します。

(継承元 ValidationAttribute)
ErrorMessageString

ローカライズされた検証エラー メッセージを取得します。

(継承元 ValidationAttribute)
Maximum

許可される最大フィールド値を取得します。

MaximumIsExclusive

Maximumと等しい値に対して検証を失敗させるかどうかを指定します。

Minimum

許容される最小フィールド値を取得します。

MinimumIsExclusive

Minimumと等しい値に対して検証を失敗させるかどうかを指定します。

OperandType

値を検証する必要があるデータ フィールドの型を取得します。

ParseLimitsInInvariantCulture

現在のカルチャではなくインバリアント カルチャを使用して、 Minimum および Maximum の文字列値を解析するかどうかを決定する値を取得または設定します。

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)

適用対象