MVC Model Annotation Validation
First build an empty solution and then build the Visual C #/Web/asp.net MVC 4 Web Application
Then build a class such as UserInfo in the MVC Models folder.
Reference namespaces
Using System.ComponentModel;
Using System.ComponentModel.DataAnnotations;
Class inside the code:
[DisplayName ("username"), Required (errormessage = "user name is NOT NULL"),
Stringlength (int. MaxValue, minimumlength = 2, errormessage = "User name must be greater than 2")]
[System.Web.Mvc.Remote ("Checkusername", "Test01", HttpMethod = "POST", errormessage = "User name already exists")]
public string UserName {get; set;}
[DisplayName ("Password"), stringlength (int. MaxValue, minimumlength = 6, errormessage = "Password must be greater than 6")]
public string Password {get; set;}
[DisplayName ("Enter password Again"), Compare ("Password", errormessage = "Confirm password inconsistent with original password")]
public string ConfirmPassword {get; set;}
[DisplayName ("Age"), Range (1, errormessage = "age must be between 1-120 years old")]
public int Age {get; set;}
[DisplayName ("Mailbox"), RegularExpression (@ "^\[email protected]\w+.\w+$", errormessage = "emial illegal")]
[EmailAddress]
public string Email {get; set;}
Build a controller and add a strongly typed view of the newly built UserInfo class in action
Embed 3 references in the view
<link href= "~/content/site.css" rel= "stylesheet"/>
<script src= "~/scripts/ Jquery-1.7.1.js "></SCRIPT>
<script src=" ~/scripts/jquery.validate.js "></SCRIPT>
< Script src= "~/scripts/jquery.validate.unobtrusive.js" ></SCRIPT>
Create a table
<div>
<form action= "/test01/add" method= "POST" >
@Html. displaynamefor (c = c.username)
@Html. textboxfor (c = c.username)
@Html. validationmessagefor (c = c.username)
<br/>
@Html. displaynamefor (c = c.password)
@Html. passwordfor (c = c.password)
@Html. validationmessagefor (c = c.password)
<br/>
@Html. displaynamefor (c = c.confirmpassword)
@Html. passwordfor (c = c.confirmpassword)
@Html. validationmessagefor (c = c.confirmpassword)
<br/>
@Html. displaynamefor (c = c.age)
@Html. textboxfor (c = c.age)
@Html. validationmessagefor (c = c.age)
<br/>
@Html. displaynamefor (c = c.email)
@Html. textboxfor (c = c.email)
@Html. validationmessagefor (c = c.email)
<br/>
<input type= "Submit" value= "register"/>
</form>
</div>
How to tell if a user name already exists such as Zhang San
and build an action in this controller. Special note: The action built here must follow the previous entity class
[System.Web.Mvc.Remote ("Checkusername", "Test01", HttpMethod = "POST", errormessage = "User name already exists")]
otherwise useless.
Inside Code:
String name = request.form["UserName"];
if (name = = "Zhang San")
{
Return Content ("false");
}
Return Content ("true");
1, model annotations, used to check the legality of the elements in the form, the effect is similar to the previous Formvalidate.js plug-ins learned
namespaces: Using System.ComponentModel; The following stores the
1, DisplayName (): Tag property to display the name of the Mate view above the @Html. Displaynamefor (c=>c. Property name) to display
namespaces: Using System.ComponentModel.DataAnnotations; The following stores the
1, Required: Used to do non-null verification checks, features: The non-string type attribute is automatically added to the Required attribute label, if the string type must be manually added
2. Stringlength: The length validation used to check the properties of a string type
3. Range: Scope validation for attributes that check the value (int,decimal,double,float) type
4, Compare: Used to compare the values of two attributes are consistent, generally used to confirm the password attribute
5, [RegularExpression ("\\d+", errormessage= "age must be a value")]: The value of this attribute must be constrained to be a number
namespaces: Using System.ComponentModel.DataAnnotations; The following stores the
1. Remote ("Requested action name", "Controller name", HttpMethod = "post/get (generally recommended to use POST request to prevent Ajax caching)", errormessage = "Prompt text") : An AJAX request is issued to an action in one of the controllers for the check operation, and if present, the return string is false (note that you cannot use uppercase false, and you cannot call it normally if you use it)
Indicates that this value already exists and cannot be reused, otherwise it returns true (note that you cannot use uppercase true if it is not called correctly) to indicate that you can use the
[System.Web.Mvc.Remote ("Checkusername"//represents the requested action name
, "Home"//on behalf of the controller name
, the httpmethod= "POST"//represents the Ajax asynchronous request to post, primarily to prevent the caching of Ajax requests, and if using get may result in inaccurate results
, errormessage= "This value has been used, please re-swap")//indicates that if the server returns a false string, the user is reminded
]
To verify the legality of page elements using required:
1. On the entity attribute [Required (errormessage= "Name property NOT NULL")]
2, on the View page using @html.textboxfor (c=>c.name): Used to produce a text box, while adding a text frame data-val= "true" data-val-* ...
3. On the view page, use @html.validationmessagefor (c=>c.name) to display the prompt text.
4, reference three JS script
<script src= "~/scripts/jquery-1.7.1.js" ></script>
<script src= "~/scripts/jquery.validate.js" ></script>
<script src= "~/scripts/jquery.validate.unobtrusive.js" ></script>
5, note: Elements must be placed in the form <form> tags to play a role in validation
6. Check the MVC site with the two nodes in the <appSettings> node in Web. config below the directory
<add key= "clientvalidationenabled" value= "true"/>
<add key= "unobtrusivejavascriptenabled" value= "true"/>
Value must be true in order to ride the verification function, otherwise any one shutdown will not work
What is a non-embedded script (non-intrusive script) ~/scripts/jquery.validate.unobtrusive.js
Does not write a validation code on the view to complete the validation of the form element, just use the relevant script to complete, this is called non-embedded
MVC Model Annotation Validation