In MVC, the validation of the front-end backend is implemented.
front-end validation . Steps:
Web. config these two have to be opened:
<add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="True " />
JS settings
First step: Introduce page JS
@Scripts. Render ("~/bundles/jqueryval")
The second step: Bundleconfig class, Must have
1 Bundles. ADD (new scriptbundle ("~/bundles/jqueryval"). Include (2 "~/scripts/jquery.validate*"));
This is the three JS
Front-end validation, implementation in MVC:
Add some features to the model, all in System.ComponentModel.DataAnnotations, such as required range.
HTMLHelp in MVC analyzes these features and loads the data-xx.
Like what:
1 [Required]2 Public string Get set; }
1 @Html. editorfor (x = x.gender)
The generated HTML
1 < class= "Text-box single-line" data-val= "true" data-val-required = "The Gender field is required. " ID=" Gender " name=" Gender " type=" text " Value= "">
Then the front-end verification is automatically implemented according to the relevant JS.
Server-Side validation:
Implementation in MVC: add some features to the model, all in System.ComponentModel.DataAnnotations, such as required range.
1 [HttpPost]2 Public ActionResult Createstudent (Studentviewmodel model)3 {4 5 modelstate.addmodelerror ("A", "AAAAA");6 modelstate.addmodelerror ("B", "bbbbb");7 modelstate.addmodelerror ("C", "CCCCC");8 modelstate.addmodelerror ("D", "ddddd");9 Ten return View (); One}
During the Modelbinder process, server-side validation is turned on. At the same time, the verification does not pass the information in the way of key-value pairs, the value is passed to Modelstate
Rendering of error messages:
@Html. ValidationSummary (False)
Whether to hide the error message: False to hide the error message, true to conceal the error message
1 @Html. validationmessagefor (x = x.name) 2 @Html. Validationmessage ("Name")
The first is the LAMDA expression, the second is the general form and the function is the same.
Render the corresponding error (depending on key, render the corresponding value)
Custom Validationattribut (server-side authentication only)
1: For an attribute
New class Myvalidationattribute, inheriting Validationattribute
1 Public classMyvalidationattribute:validationattribute2 {3 PublicMyvalidationattribute ()4 {5ErrorMessage ="The Name must be Zhangsan";6 }7 Public Override BOOLIsValid (Objectvalue)8 {9 if(value==NULL)Ten { One return false; A } - stringresult =value. ToString (); - //determine if there is a value the if(string. IsNullOrEmpty (Result)) - { - return false; - } + - if(result=="Zhangsan") + { A return true; at } - - return false; - } -}
Application:
1 Public classStudentviewmodel2 {3 Public stringID {Get;Set; }4 [Myvalidationattribute]5 Public stringName {Get;Set; }6 [Required]7 Public stringGender {Get;Set; }8}
View:
1 <Div>2 @using (Html.BeginForm ())3 {4 @Html. ValidationSummary (False)5 <fieldset>6 <legend>UserInfo</legend>7 <Divclass= "Editor-label">8 @Html. labelfor (x = x.name)9 </Div>Ten <Divclass= "Editor-field"> One @Html. editorfor (x = x.name) A @Html. validationmessagefor (x = x.name) - @Html. Validationmessage ("Name") - </Div> the - <Divclass= "Editor-label"> - @Html. labelfor (x = x.gender) - </Div> + <Divclass= "Editor-field"> - @Html. editorfor (x = x.gender) + @Html. validationmessagefor (x = x.gender) A </Div> at </fieldset> - <inputtype= "Submit"value= "Submit" /> - } - </Div>
Effect:
2: Complex business logic
Class Studentviewmodel inherit interface ivalidatableobject
Public classStudentviewmodel:ivalidatableobject { Public stringID {Get;Set; } [Myvalidationattribute] Public stringName {Get;Set; } [Required] Public stringGender {Get;Set; } PublicIenumerable<validationresult>Validate (Validationcontext validationcontext) {if(!string. IsNullOrEmpty (Name)) {if(name=="Zhangsan"&& gender=="Nan") { yield return NewValidationresult ("Zhangsan and Gender is Nan,which are wrong!"); } } } }
:
MVC Learning Series--model Validation Extensions