In this tutorial, we are going to learn ASP.NET MVC Validation. Validation plays a vital role in ASP.NET MVC Applications. Validation is used to check the user input is valid or not. It is very easy to use and easy to implement.
Don’t Repeat Yourself
DRY (Don’t Repeat Yourself ) is one of the key design principles of ASP.NET MVC. If we talk about development point of view, it is encouraged to specify functionality only at one place and then it is used in the entire application from that one place. It helps you in terms of reducing the amount of code you need to write and make the code you do write less error-prone and easier to maintain.
ASP.NET MVC Data Annotation Validator Attributes
There are following different types of ASP.NET MVC data annotation Validator Attributes.
Required: It enables you to mark a property as required.
Range: It enables you to validate whether the value falls within a specified range of values.
RegularExpression: It enables you to validate whether the value matches a specified regular expression pattern or not.
StringLength: It enables you to specify a maximum length for a string property.
MaxLength: It specifies the maximum length of the array.
Validation: It serves as the base class for all the validation attributes.
Compare: It provides an attribute that compares two values.
Display: It provides an attribute that displays property name on view.
DisplayFormat: It specifies how data fields are displayed and formatted by ASP.NET Dynamic data.
DataType: It enables you to specify the data type of the model.
ASP.NET MVC VALIDATION
Let’s implement these ASP.NET MVC Validation attributes in a simple application by creating a new project.
Step # 1: Open Visual Studio. Go to File => New => Project. It will show a new project template.
Step # 2: Choose the web from the left pane under the visual C#, then choose ASP.NET Web Application from the middle pane, then enter the name (E.g. “ValidationDemo”) in the name field and then finally click ok button. It will show a new dialog.
Step # 3: Choose MVC Template and check the MVC checkbox and then click ok button
Adding model to the application.
Step # 4: Right click on the Model folder from the solution explorer, then choose add and then choose Class. It will show a new “Add New Item – ValidationDemo” dialog.
Step # 5: Enter the name (E.g. Student.cs) in the name field. And then click Add button.
Then you will see Student.cs class under the Model folder in the Solution Explorer.
Step # 6: To import the DataAnnotation attributes we need to Enter the namespace “ using System.ComponentModel.DataAnnotations;” in the Student.cs model class.
Step # 7 : Write the following properties in the Student.cs class.
Step # 8 : Now add the following DbContext class in Student.cs mode class.
Step # 9: To import the DbContext Class we need to enter the “using System.Data.Entity;” namespace in the Student.cs model class.
Let’s add a controller. Just right click on the controller folder in the solution explorer, then Add, and then choose controller.
Now choose “MVC 5 Controller – Empty” and then click “Add” button.
Enter the name in the name field and then click Add button.
Then you will see the following index method in StudentControlller.cs class.
Let’s add a view to this Index Action Method. Just right click on the index method, then choose “Add View” Then you will see an “Add View” dialog. Now select the Create Template from the Template drop-down list, and then choose Student as Model class from the Model Class template as we have created this model. And finally select the StudentDBContext class from the Data Context Class drop-down list. And then click Add button.
Then you will see the following view code.
Now let’s run your application with this URL “ http://localhost:58036/Student/Index”and check whether validation is working or not.
So finally we have successfully created Data Annotations Validation in our project using asp.net MVC. I hope it will be useful. Please like and share this post if you like…!