Code: http://down.51cto.com/data/187446
Before reading this article, I hope you can read it first. I will use jquery plug-in author blog: http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
Download the package and decompress it to a certain place.
1. Create an asp.net mvc 3 Project:
2. Add the required js and css files to the project (in the directory you just decompressed ).
3. Create a model named user, which contains three attributes. All attributes are required.
Then we create the corresponding Controller
4. Html Helper Extension
This is the most exciting part. We have extended the html helper and used reflection to find whether the current Model attribute is required. If required, we will write an html code to the page as a prompt for verification.
5. Add js and css to the template. The layout page is in Razor:
The last part is our view:
@ Html. Editor () This extension method will generate html. The important thing is that we need to use the bottom section to verify the current form, through form id ....
Run the following command to view the result:
Amazing
This is a good verification solution for the entire project. Of course, it can also verify other controls. For details, refer to the example in the code you downloaded.
Link: http://www.cnblogs.com/n-pei/archive/2011/04/11/2011964.html