From http://firechun.blog.163.com/blog/static/31804522201103133832931/
There is a serious problem in the edit and create forms: no verification is performed. We can try to leave the required field blank or enter a letter in the price field. We will see an error from the database.
For applicationsProgramAdding data annotations to the model class makes it easy for us to verify the data. Data annotations allow us to describe the rules to be applied to model attributes, Asp. net MVC enforces these rules and displays appropriate messages to users.
Add verification for album forms
We will use the following data annotation features:
- Required -- indicates that this field is mandatory.
- Displayname -- defines the text to be displayed in form fields and verification information
- Stringlength -- defines the maximum length of a string attribute.
- Range -- defines the maximum and minimum values for Numeric Fields
- Bind -- list the fields that include or exclude when a parameter or form data is bound to a model property.
- Scaffoldcolumn-fields allowed to be hidden in the editor form
Note: For more information about using the data Annotation Feature for model verification, visit the msdn documentation:Http://go.microsoft.com/fwlink? Linkid = 159063
Open the album class and add the following namespace at the top:
Using system. componentmodel;
Using system. componentmodel. dataannotations;
Using system. Web. MVC;
Add display and verification features for properties as follows:
Namespace mvcmusicstore. Models
{
[BIND (exclude = "albumid")]
Public class album
{
[Scaffoldcolumn (false)]
Public int albumid {Get; set ;}
[Displayname ("genre")]
Public int genreid {Get; set ;}
[Displayname ("artist")]
Public int artistid {Get; set ;}
[Required (errormessage = "an album title is required")]
[Stringlength (160)]
Public String title {Get; set ;}
[Required (errormessage = "price is required")]
[Range (0.01, 100.00,
Errormessage = "price must be between 0.01 and 100.00")]
Public decimal price {Get; set ;}
[Displayname ("album art URL")]
[Stringlength (1024)]
Public String albumarturl {Get; set ;}
Public Virtual Genre genre {Get; set ;}
Public Virtual artist {Get; set ;}
}
}
After adding these in the class, the create and edit interfaces immediately verify the field and use the display name we defined (displayname). For example, albumarturl is replaced with album art URL. Run the application and browse/storemanager/create:
When you click "save", the verification error message is displayed next to the fields that do not comply with the verification rules.
Test client Verification
From the application perspective, server-side authentication is very important because users may bypass client-side authentication. However, server verification for web forms only has three major problems:
- 1. During form submission, server-side verification, and return to the user's browser, the user must wait.
- 2. Users cannot get feedback in time when they enter fields correctly and pass the verification rules.
- 3. We waste server resources to execute verification logic without using users' browsers.
Fortunately, the ASP. net mvc template architecture has built-in client verification and does not require any additional work.
Enter a single character in the title field for non-empty verification, and the verification will immediately disappear (proving that it is client verification ).