ASP. net mvc music store-6. Use dataannotations for Model Verification

Source: Internet
Author: User
ArticleDirectory
    • Add verification for album forms
    • Test client Verification

From http://www.cnblogs.com/haogj/archive/2011/11/16/2251920.html

There is a problem in the previous form for creating and editing an album: We didn't perform any verification. The field content can be left blank, or enter some characters in the price field.ProgramThese errors will cause errors in the database storage process, and we will see the error information from the database.

By adding dataannotations of data descriptions to the model class, we can easily add verification functions to the application. Dataannotations allows us to describe the verification rules we want to apply to model attributes. ASP. net mvc will use these dataannotations and then return the appropriate verification information to the user.

Add verification for album forms

We will use the following dataannotations

    • Required required-indicates that this attribute is a field that must provide content
    • Displayname display name-defines the prompt name of the form field
    • Stringlength String Length-defines the maximum length of a string type attribute
    • Range-provides the maximum and minimum values for numeric attributes.
    • Bind binding-lists the fields that are included and not included when the request parameters are bound to the model.
    • Scaffoldcolumn bracket column-characters to be hidden when editing a form

Note: For more information about model verification, see: http://msdn.microsoft.com/zh-cn/library/ee256141%28VS.100%29.aspx

To open the album class, first Add the following using statements that reference the namespace used by dataannotations.

 
UsingSystem. componentmodel;
UsingSystem. componentmodel. dataannotations;
UsingSystem. Web. MVC;

 

Then, update the attributes and add the display and verification dataannotations.

 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 ;}
}
}

 

Then, set the attribute genre and artist of the album to virtual, which will cause ef-code first to use delayed loading.

Public VirtualGenre genre {Get;Set;}
Public VirtualArtist {Get;Set;}

 

After the modification is complete, the field will be verified immediately on the create and edit interface, and the display name provided by us will be used. For example, albumarturl will become album art URL and so on. Run the program, browse/storemanager/create.

Next, we enter some data that violates the verification rules, enter 0 in the price field, and leave the content of the title field blank. When we click Create, the validation error message is displayed for fields that do not comply with the validation rules in the form.

Test client Verification

For applications, server-side verification is very important because the user may bypass client verification. In fact, there are three significant problems for only server-side verification on web pages:

    1. When submitting a form, you must wait for the verification to be performed on the server. You must send the verification result back to the browser.
    2. The user cannot receive a response immediately when an error is entered to pass the verification rule check.
    3. We handed over the work that can be done in the browser to the server, wasting the server resources.

Fortunately, the ASP. NET mvc3 support template also provides built-in client verification, which can be used without additional work.

Enter a character in the required title field. The verification error message disappears immediately.

Here, we should pay attention to the following points:

The jquery script has been referenced in the page.

<ScriptSRC= "@ URL. Content ("~ /Scripts/jquery. Validate. Min. js ")" Type= "Text/JavaScript"> </Script>
<ScriptSRC= "@ URL. Content ("~ /Scripts/jquery. Validate. unobtrusive. Min. js ")" Type= "Text/JavaScript"> </Script>

In web. config, client verification is supported by default.

<Appsettings>
<AddKey= "Clientvalidationenabled"Value= "True"/>
<AddKey= "Unobtrusivejavascriptenabled"Value= "True"/>
</Appsettings>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.