ASP.NET MVC validation attributes
Apply validation rules to the model support; a SysTem.CompontModel.DataAnnotions namespace; ValidationAttribute inherits from class, the basic properties of a message used to define the attributes ErrorMessage
Required: Required verification
Compare: Compare verify, verify that the two values match
StringLength: authentication string length (the maximum length of the individual may be provided, may be provided while minimizing the length)
Range: for digital (integers and floating point), and other types of time data range validation
RegularExpression: Regular expression validation
First, the definition of validation rules
1, the reference in the Models:
Framework: System.Web
Extended: System.CompanentModel.DataAnnotaions
Framework: System.Web.Mvc
2, the entity class references:
using System.CompanentModel
using System.CompanentModel.DataAnnotaions
using System.Web.MVC
Second, the application validation rules
1, a plurality of rules can be bound by the same property. Disposed in front of the class attributes.
Text on [the DisplayName ( "Age")] placeholder
[Required (ErrorMessage = "{0} is not empty")]
[Range (18,60, ErrorMessage = "{0} must {1} and {2} between")]
? Public int Age {get; set;} represents null type "?"
2, use strong typing Razor view engine;
3, with the view names subject attribute name is exactly
4, the controller in combination with a database verification
Forms and model validation relationships: the definition of validation rules in the model, ASP.NET MVC when mapping the data to the model, validation rules are automatically applied based on the model;
Verification process, will automatically add the validation error messages to ModelState data dictionary.
ModelState common members:
isValid: verify the classification model by all
AddModelError (key.value): Add a custom error messages
Third, verify the information displayed
1, in two ways:
Method a: ModelState theoretically possible to use the authentication information object to output, but more complicated;
Method two: in practice, the use of two Html object methods to help verify the information output
MvcHtmlString ValidationMessage (string modelName): Html view helper. Displays the authentication information field (text box displayed behind the verification information, or below, in a location to be displayed on it)
MvcHtmlString ValidationSummary (): show all fields authentication information.
Reproduced in: https: //www.cnblogs.com/atlj/p/8876329.html