Data annotations for model classes

Source: Internet
Author: User

Validation support in ASP. (provided by the. NET Framework and EF) is a good example of dry (Don ' t Repeat yourself) principle practice. As long as the annotation properties are declared in the Model class for fields and properties, they can be applied to all views that use the model, and data annotations allow you to specify the data display format, validation rules, and database mapping rules for the model class.

The System.ComponentMode.DataAnnotations namespace provides some built-in properties that allow you to customize error messages.

Validation attribute: Required, definition cannot be null, stringlength, character length, regularexpression, regular expression; Range ().

Show Properties: Display (name=), define display field names, Dispalyformat (), display format.

Database Mapping: Column ("") is mapped to the specified column name, column (typename= "") is mapped to the specified type in the database when the type in the database is more specific than the type in C #.

Other: DataType (datatype.xx) enables some features of the browser to support HTML5.

The code is as follows:

public class Movie
{
public int ID {get; set;}

[Display (name= ")]//Define the field names displayed in the Web page.
[Required (allowemptystrings=false,errormessage= "{0} cannot be empty")] //value type (decimal,int, etc.) default cannot be empty, you can customize the error message via the required annotation property, ErrorMessage {0} represents the Loadline name, if declared in the display (name= ") property Use a name value, such as "slice name" in this example.

[Stringlength (20,minimumlength=3,errormessage= "{0} must be greater than {2}, less than {1}")]//stringlength annotation Property The first parameter is the maximum field value, which changes the structure of the database. Minimunmlength does not change the structure of the database, but it is applied to client and server-side validation. {0} in errormessage represents the Loadline name, {1} represents the maximum value of the first parameter of Stringlength, and {2} represents the second parameter minimumlength of Stringlength;
public string Title {get; set;}

[Display (name= "Release date")]
[DataType (Datatype.date)]//using the browser support HTML5 properties, here is a date marquee, do not change the database [DisplayFormat (dataformatstring = "{0:YYYY-MM-DD}" , Applyformatineditmode = True)]//Specifies the display format.
Public DateTime releasedate {get; set;}

[Display (name= "movie category")]
[Required (errormessage= "{0} cannot be empty")]
[Stringlength (30)]
public string Genre {get; set;}

[Required (allowemptystrings=false,errormessage= "{0} required")]

[Display (name= "price")]
[Column (typename= "money")] //Specifies the column name that the field maps to the database table.
[Range (1,1000,errormessage= "{0} must be greater than {1} and less than {2}")] //{0} represents the display name of the field, {1} is the first parameter of range, and the minimum value. {2} is the second parameter of range, maximum value.
[DataType (Datatype.currency)]//using the browser to support HTML5 properties, here is a local-based currency symbol appears.
Public decimal price {get; set;}

[Display (name= "movie rating")]
[Required (errormessage= "{0} cannot be empty")]
[Stringlength (6,errormessage= "{0} cannot exceed 6 characters")]
public string Rating {get; set;}
}

The IF (modelstate.isvalid) in the controller method is the model binder that checks whether the data is legitimate, and it supports both client and browser validation with the built-in properties. If an error occurs, first

Data annotations for model classes

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.