Effect of Data Annotation in ASP.NET MVC

Tram Ho

In Asp .net mvc we often see some pairs of [] inside which define some content, that is, using data annotation. So what is annotation? How to use it? How does it work? Let’s find out in this article ?

I. The concept of data annotation in ASP.NET MVC

  • In the .NET Framework, Data Annotation is used to add the meaning extension to data through attribute tags. The advantage of using Data Annotation feature is that we can manage the data defined in a location or not need to rewrite many laws on many different positions.
  • The Data Annotation feature was first introduced in .NET 3.5 in the System.ComponentModel.DataAnnotations namespace. This namespace contains classes used to define extended attributes for data.
  • Data Annotation attributes are divided into three main categories:
    • Validation Attribute: used to add validation rules for data
    • Display Attribute: Used to specify how data from a class is displayed in the interface.
    • Modeling attribute: used to specify the purpose of a member’s class and the relationships between classes.
  • The effect of data annotation:
    • Data Annotation in Entity Framework helps us to define attribute data directly in source code sown from database tables and helps you control the input data from the best users as well as how Data definition is displayed on the interface.
  • How to use data annotation in ASP .NET MVC:
    • When we use these properties, we must put them in [] and must put them above the field we want to validate or define.

II. Practice defining data annotations in an ASP .NET MVC application

1. Preparation

  • Initialize an asp .net mvc project

-Go to Sql Sever to create a database

Go to Project => Manager nuget package => Select the browse tab and find the entity framework as shown:

Then connect to the newly created database

Add the following XML to the configuration tag in the web.config file (your computer’s connectionString will be different)

2. Initialize the model, controller and view

  • Go to the model directory and create an Employee.cs file

  • Next we add a folder named Context to the project to contain the database connection contexts, create the EmployeeContext.cs file inside and configure the following

  • Rebuild the project
  • Right-click controller directory => add => Add new scaffolded Item => MVC 5 Controller with view, using entity framewrok
  • Run the app and get the results:

The employee table has been created on the database

3. Practice using Data Annotation

  • Return to Model Employee.cs in the model directory

  • I have used it here
    • DisplayName: Defines the column name in the table
    • Required: Required string is a required field
    • StringLength: Defines the maximum length for the data field
    • Range: Set min max for value
    • DataType: validate the data entered correctly according to the predefined DataType (date, EmailAddress …)
    • RegularExpression: Checks the input string in a self-defined regular expression
    • There are many other properties you can access the docs annotation of microsoft to find out.
  • Because the model has been changed, in order for the database to change, we must use the migration mechanism. Go to tool => Nuget package manager => Package manager console Execute 3 sentences in turn:

Next, delete the controller and the corresponding views of Employee and recreate it because you changed the structure of the model

  • Finally run the app and check out the changes you’ve made
    • Page display
    • The input form has been validated

III. summary

In my opinion, the annotation is a very good library, it just helps us validate easily, and also has a high security.

People give me comments to change better as well as motivation for me to do the next article nhé

Share the news now

Source : Viblo