1. Learn about the powerful validation features in MVC with an example
1.1 Create a "basic" MVC project, because you want to use the validation of JS, and then write the code in a view, and the code in the model is as follows
The code in model
1.2. Start testing
1.3 Viewing source code
<!DOCTYPE HTML><HTML><Head> <Metaname= "Viewport"content= "Width=device-width" /> <title>Add</title> <Scriptsrc= "/scripts/jquery-1.8.2.js"></Script> <Scriptsrc= "/scripts/jquery.validate.js"></Script> <Scriptsrc= "/scripts/jquery.validate.unobtrusive.js"></Script></Head><Body><formAction= "/home/modify"Method= "POST"> <Table> <TR> <TD>Name</TD> <TD><inputData-val= "true"Data-val-length= "Field name must be a string with a maximum length of 3. "Data-val-length-max= "3"ID= "SName"name= "SName"type= "text"value="" /> <spanclass= "Field-validation-valid"data-valmsg-for= "SName"Data-valmsg-replace= "true"></span> </TD> </TR> <TR> <TD><label for= "SAge">Age</label></TD> <TD><inputclass= "Text-box single-line"Data-val= "true"Data-val-number= "field age must be a number. "Data-val-range= "This position fills >=1 and <=99 value"Data-val-range-max= " the"Data-val-range-min= "1"ID= "SAge"name= "SAge"type= "text"value="" /> <spanclass= "Field-validation-valid"data-valmsg-for= "SAge"Data-valmsg-replace= "true"></span> </TD> </TR> <TR> <TD></TD> <TD><inputtype= "Submit"value= "Submit"/></TD> </TR> </Table></form><!--Visual Studio Browser Link -<Scripttype= "Application/json"ID= "__browserlink_initializationdata"> {"AppName":"Firefox","RequestID":"DA5289DA760B44F8B8C56A9684E0A5BD"}</Script><Scripttype= "Text/javascript"src= "Http://localhost:6217/e3c73d9e3ac34dfa9df5109ec8422564/browserLink"Async= "Async"></Script><!--End Browser Link -</Body></HTML>
Originally, these text boxes themselves did not change, just a few more properties, when the introduction of the validation of the JS file, based on the attributes on the text box, add the corresponding JS operation. When the discovery condition is not satisfied, the form is not submitted to the server, which is actually done as follows. Set the OnSubmit property in the form to return a false so that the form is not submitted.
2. Talk about features
is a feature that modifies the sname attribute and some description of the attribute
3. Other
Some additional instructions for the DataType enumeration
MVC Series Learning (11)-Validation