In this Video, I am going to give you a brief introduction to Data Annotation in MVC Application which is used for validating the data of a model. Please watch our previous section where we discussed Attributes in ASP.NET MVC Application.
Why do we need Data Annotation Attributes in ASP.NET MVC?
Now a days, its a challenging job for a web developer to validate the user input for any Web application. As web developers, we not only validate the business logic at the client-side that is in the browser, but also we need to validate the business logic running on the Server. That means as a developer we need to validate the business logic both at the client-side as well as server-side.
The client-side validation of the business logic gives the users immediate feedback on the information they entered into a web page and which is an expected feature in todays web applications. Along the same line, the server-side validation logic is in place because we never trust the information coming from the network.
What are the Validations?
In simple terms, we can say that validations are nothing but some rules set by the developer on the input fields of a web page so as to satisfy the business rules for that particular input field in order have to maintain the proper data in a system. There are two types of validations:
- Server-side Validations
- Client Side Validations
While doing validations, as a developer we need to take care of not only the proper validation but also ensure that the validation meets the business rule as per the requirement. From the security point of view, it is also possible that some hackers may bypass client-side validation and insert some vulnerable data into the server.
When we talk about the validation in the ASP.NET MVC framework, we primarily focus on validating the model value. That means has the user provided a required value? Is the value in the required range? Is the value in a proper format etc.?
In ASP.NET MVC web applications, we can do the following three types of validations:
- ASP.NET MVC Model validation (i.e. Server-side Validation)
- Database validation (i.e. Server-side Validation)
ASP.NET MVC Framework provides a concept called Data Annotation which is used for model validation. Its inherited from System.ComponentModel.DataAnnotations assembly.
That means ASP.NET MVC Framework uses Data Annotation attributes to implement model validations. The Data Annotation Attributes include built-in validation attributes for different validation rules, which can be applied to the model class properties.
The ASP.NET MVC framework will automatically enforce the validation rules and then display proper validation messages in the view if validation fails.
The System.ComponentModel.DataAnnotations assembly has many built-in validation attributes, for example:
- Data type
Along with the above build-in validation attributes, there are also many data types the user can select to validate the input. Using this data type attribute, the user can validate the exact data type as in the following:
- Credit Card number
- Email Address
- Image URL
- Multiline text
- Phone number
- Postal Code
Lets create an empty ASP.NET MVC application with the name DataAnnotationInMVC.
Next, add the Employee.cs file to the Models folder and copy-paste the following code
Now Add EmployeeContoller to the Controllers folder and copy-paste the following code. In the below controller, we have created three action methods i.e. Index and Create action method with Get and Post HTTP Verb.
Create the Create.cshtml view
Right-click on the Create action method of the Home Controller class from the context menu and click on Add View. Once you click on the Add View option then it will show you the below popup.
Then click on the Add button which will create the Create.cshtml view in Home Folder which is inside the Views folder. Copy and paste the following code in the Create.cshtml view
We have added Create.cshtml view to create the Action Method when we run the application and go to the “/Employee/Create” URL it will look as shown below:
Enabling Client-Side Validation in ASP.NET MVC Application: