Data Annotations in ASP.NET Core
Data annotation can be used most commonly in in ASP.NET Core MVC or ASP.Net Data
controls to perform model validation. Data annotations (available as part of the
System. ComponentModel. DataAnnotations namespace, are attributes that can be
applied to classes or class members to specify the relationship between classes,
describe how the data is to be displayed in the UI, and specify validation
rules. This article briefly tells you about data annotations, why they are
useful, and how to use them in our .NET Core applications.
Types of Data Annotations
DA attributes are used to specify metadata on a class or a property. The data
annotation attributes can broadly be classified into the following:
- Validation attribute — Used to enforce validation rules on the properties of the entities
- Display attribute — Used to specify how the data should be displayed in the user interface
- Modeling attribute — Used to specify the relationship that exists between classes Attribute classes
- DataType- to specify datatype rules for a specific data type like Date etc
- Key – specify the property as a Key/Primary Key
- MaxLength
- Required
- StringLength
- Timestamp
- IsEmpty Required
public class Customer
{
[Required(ErrorMessage = "{0} is required")]
[StringLength(50, MinimumLength = 3, ErrorMessage = "First Name
should be minimum 3 characters and a maximum of 30 characters")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[Required(ErrorMessage = "{0} is required")]
[StringLength(50, MinimumLength = 4, ErrorMessage = "Last Name
should be minimum 3 characters and a maximum of 30
characters")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[DataType(DataType.PhoneNumber)]
[Phone]
public string PhoneNumber { get; set; }
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
}
class Program { static void Main(string[] args)
{
Customer cust = new Customer();
cust .FirstName = "Aisha";
cust.LastName = ""; //invalid value entered
cust .PhoneNumber = "1234567890"; cust .Email =
"abc@gmail.com";
//use this code to validate the customer object for the
data
ValidationContext context = new ValidationContext (author, null,
null);
List
validationResults = new List();
bool valid = Validator.TryValidateObject (author, context,
validationResults, true);
if (!valid) {
foreach (ValidationResult validationResult in
validationResults)
{
Console.WriteLine("{0}",
validationResult.ErrorMessage);
}
Console.ReadKey();
}
}
On execution you get the following error:
LastName is required
For IsEmpty rule validation you can use following data annotation:
[IsEmpty(ErrorMessage = "Should not be null or empty.")]
public string Name{ get; set; }
You can take advantage of data annotations to define data validation
rules in a single place and thereby avoid having to re-write the same
validation code again and again. Data annotation can be used most
commonly in in ASP.NET Core MVC or ASP.Net Data controls to perform
model validation.
Comments
Post a Comment