Adding Validation to our Album Forms
We’ll use the following Data Annotation attributes:
- Required – Indicates that the property is a required field
- DisplayName – Defines the text we want used on form fields and validation messages
- StringLength – Defines a maximum length for a string field
- Range – Gives a maximum and minimum value for a numeric field
- Bind – Lists fields to exclude or include when binding parameter or form values to model properties
- ScaffoldColumn – Allows hiding fields from editor forms
示例:
[ScaffoldColumn(false)] //这个就是是否自动插入隐藏值
[DisplayName("书号")]
[Required(ErrorMessage="书号是必须的")]
[StringLength(160)] //长度最长为160
[Required(ErrorMessage="Price is required")]
[Range(0.01,100.00,ErrorMessage="价格必须在 0.01-- 100.00之间")]