StringLengthAttribute 類別

定義

指定資料欄位中允許的最小與最大字元長度。

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

範例

以下範例說明如何限制欄位中的字元數量。 此範例可搭配 AdventureWorksLT 資料庫中 Products 表格中的 ThumbnailPhotoFileName 欄位操作。 欄位限制為 4 個字元,透過將屬性套用 StringLengthAttribute 到代表 Product 實體的部分類別。

[MetadataType(typeof(ProductMetadata))]
public partial class Product
{

}

public class ProductMetadata
{

    [ScaffoldColumn(true)]
    [StringLength(4, ErrorMessage = "The ThumbnailPhotoFileName value cannot exceed 4 characters. ")]
    public object ThumbnailPhotoFileName;

    [ScaffoldColumn(true)]
    [StringLength(4, ErrorMessage = "The {0} value cannot exceed {1} characters. ")]
    public object PhotoFileName;

}
<MetadataType(GetType(ProductMetadata))> _
Public Partial Class Product

End Class

Public Class ProductMetadata

    <ScaffoldColumn(True)> _
    <StringLength(4, ErrorMessage := "The ThumbnailPhotoFileName value cannot exceed 4 characters. ")> _
    Public ThumbnailPhotoFileName As Object

    <ScaffoldColumn(True)> _
    <StringLength(4, ErrorMessage := "The {0} value cannot exceed {1} characters. ")> _
    Public PhotoFileName As Object

End Class

備註

ASP.NET 動態資料啟用資料模型中的 CRUD(建立、讀取、更新與刪除)操作。 你可以在資料插入或更新時,指定每個欄位的最小與最大字元長度。 對於字元資料型別, MinimumLengthMaximumLength 屬性表示儲存字串所需的最大位元組數。

你可以在錯誤訊息中使用 複合格式 的佔位符: {0} 是屬性名稱; {1} 是最大長度; {2} 是最小長度。 佔位符對應於執行時傳遞給 String.Format 方法的參數。

建構函式

名稱 Description
StringLengthAttribute(Int32)

透過使用指定的最大長度初始化類別的新 StringLengthAttribute 實例。

屬性

名稱 Description
ErrorMessage

若驗證失敗,會接收或設定錯誤訊息以關聯驗證控制項。

(繼承來源 ValidationAttribute)
ErrorMessageResourceName

取得或設定錯誤訊息資源名稱,以便在驗證失敗時查詢 ErrorMessageResourceType 屬性值。

(繼承來源 ValidationAttribute)
ErrorMessageResourceType

取得或設定資源類型,以便在驗證失敗時用於錯誤訊息查詢。

(繼承來源 ValidationAttribute)
ErrorMessageString

會收到本地化驗證錯誤訊息。

(繼承來源 ValidationAttribute)
MaximumLength

取得或設定字串的最大長度。

MinimumLength

取得或設定字串的最小長度。

RequiresValidationContext

會得到一個值,表示該屬性是否需要驗證上下文。

(繼承來源 ValidationAttribute)
TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)

方法

名稱 Description
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)

明確介面實作

名稱 Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取 物件的型別資訊,可用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開屬性和方法的存取權。

(繼承來源 Attribute)

適用於