Model Verification in ASP. NET mvc3

Source: Internet
Author: User
[Transferred from: http://blog.csdn.net/w809038518/article/details/6754157]

Directory (?)[-]

    1. Overview
    2. Data-level verification
    3. Business logic verification
    4. Summary

Overview

In the previous section, we learned how to transfer model data between interfaces. However, in many cases, to ensure data validity, you have to perform basic data verification on the attributes of the model.

This section describes how to use the features in the system. componentmodel. dataannotations namespace to specify the verification of each field in the data model.

These features are used to define common verification modes, such as range checks and mandatory fields. The dataannotations feature enables MVC to provide client and server verification checks, so that you do not need to perform additional encoding to control the effectiveness of data.

The system. componentmodel. dataannotations feature can be used for Entity Data Models (EDM), LINQ to SQL, and other data models. You can also create custom verification features.

For more information about dataannotations, see system. componentmodel. dataannotations Overview

Data-level verification

Create a project and create a model class named user

 
Public ClassUser {Public IntId {Get; Set ;}}

Take create verification as an example to learn dataannotations verification.

Create method

// Create// Get:/user/createPublicActionresult create (){ReturnView ();}

Add View

Note: When adding a view, if the model cannot be found for a strong view, we recommend that you regenerate the solution.

Non-empty Verification

 

 
Public ClassUser {Public IntId {Get; set;} [displayname ("Name")] [Required (errormessage ="Name cannot be blank")]Public StringName {Get; Set ;}}

Add view to run directly

 

 

 

Character length Verification

Public ClassUser {Public IntId {Get; set;} [displayname ("Name")] [Required (errormessage ="Name cannot be blank")]Public StringName {Get; set;} [displayname ("Password")] [Stringlength (6, errormessage ="The password cannot exceed 6 characters")]Public StringPassword {Get; Set ;}}

Run directly after adding a view

 

Digit Verification

 
[Displayname ("Age")] [Range (1,Int. Maxvalue, errormessage ="Enter a number greater than or equal to 1")]Public IntAge {Get; set ;}

Add view to run directly

Regular Expression Verification

 
[Displayname ("Email")] [Regularexpression (@ "^ \ W + (-\ W +) | (\. \ W +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $", Errormessage ="Enter the correct email format \ n example: abc@123.com")]Public StringEmail {Get; set ;}

Running effect after adding View

Business logic verification

Remote Server Verification

For remote asynchronous request verification, obtain the specified method verification in the specified controller during [httpget]. This method must be [Httpget]. Return JavaScript objects of the JSON type.

ModelCode

 

[Displayname ("Name")] [Required (errormessage ="Name cannot be blank")] [Remote ("Getuser","User", Errormessage ="This name already exists")]Public StringName {Get; set ;}

Controller code

 
// Httpget is required[Httpget]PublicActionresult getuser (StringName ){ReturnJSON (name! ="AA", Jsonrequestbehavior. allowget );}

Directly add view run

Custom attbitue Verification

Model Code

[Required] [stringlength (15)] [loginunique]Public StringLogin {Get; set ;}

Attribute code

[Attributeusage (attributetargets. FIELD | attributetargets. Property, allowmultiple = False , Inherited =True )] Public   Sealed   Class Loginuniqueattribute: validationattribute { Private   Static   Readonly   String Defaulterrormessage = "Login unique" ; // Mui. login_unique;          Public Loginuniqueattribute (): Base (Defaulterrormessage ){} Public   Override   String Formaterrormessage ( String Name ){ Return Defaulterrormessage ;} Public   Override   Bool Isvalid ( Object   Value ){ Return Userservice. Check (P => P. Name = Value . Tostring ());}}

Directly add view run

Summary

In fact, Microsoft dataannotations verification has been fully applied on the vsplatform, regardless of the webProgramThe wform program and even the Silverlight program can all use the dataannotations verification improved by Microsoft, which can be said to be pervasive. However, for these three programs, the MVC verification is relatively complete and simple to use.

Directly add view run

Summary

In fact, Microsoft's dataannotations verification has been fully applied on the vsplatform. Both web programs, wform programs, and even Silverlight programs can be verified using the dataannotations improved by Microsoft, it can be said that it is pervasive. However, for these three programs, the MVC verification is relatively complete and simple to use.

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.