ASP.NET MVC5中Model層開發(fā),使用的數(shù)據(jù)注解有三個作用:
數(shù)據(jù)映射(把Model層的類用EntityFramework映射成對應(yīng)的表)
數(shù)據(jù)驗證(在服務(wù)器端和客戶端驗證數(shù)據(jù)的有效性)
數(shù)據(jù)顯示(在View層顯示相應(yīng)的數(shù)據(jù))
數(shù)據(jù)注解相關(guān)的命名空間如下:
System.ComponentModel.DataAnnotations
System.ComponentModel.DataAnnotations.Schema
System.Web.Mvc
System.Web.Security
DataAnnotations名稱空間中包含主要的數(shù)據(jù)注解,Schema名稱空間中包含一些數(shù)據(jù)映射的數(shù)據(jù)注解,Mvc名稱空間中包含字符、數(shù)組、數(shù)值長度和屬性比較的數(shù)據(jù)注解,Security名稱空間中包含MemberShipPassword注解特性。
數(shù)據(jù)映射和驗證相關(guān)的數(shù)據(jù)注解:
| [Required] | 必填字段 |
| [MaxLength] | 指定屬性中允許的數(shù)組或字符串數(shù)據(jù)的最大長度 |
| [MinLength] | 指定屬性中允許的數(shù)組或字符串數(shù)據(jù)的最小長度 |
| [StringLength] | 指定最小和最大字符長度 |
| [Range] | 指定數(shù)值范圍 |
數(shù)據(jù)驗證相關(guān)的數(shù)據(jù)注解:
| [Remote] | 使用 jQuery 驗證插件遠程驗證程序的特性 |
| [FileExtension] | 驗證文件擴展名 |
| [Compare] | 比較兩個屬性的值 |
| [RegularExpression] | 使用正則表達式驗證 |
| [CustomValidation] | 自定義驗證方法 |
| [DataType] | 指定要與數(shù)據(jù)字段關(guān)聯(lián)的附加類型的名稱 |
| [EmailAddress] | 電子郵件地址(相當于[DataType(DataType.Email)]) |
| [Phone] | 電話(同上) |
| [CreditCard] | 信用卡號碼(同上) |
| [Url] | 驗證URL(同上) |
| [MemberShipPassword] | 驗證密碼字段是否滿足成員資格提供程序的當前密碼要求 |
數(shù)據(jù)映射相關(guān)的數(shù)據(jù)注解:
數(shù)據(jù)顯示相關(guān)的數(shù)據(jù)注解:
新聞熱點
疑難解答
圖片精選