Skip to main content

Data Validation in ASP.NET MVC


Data Validation in ASP.NET MVC

In this article, we will learn how to implement data validations in the ASP.NET MVC application.
In our previous article, we have created an application that performs Create, Edit, Delete and Details operations that generate views for each of the operations.
We will create new class called Blog and will implement data validation in the class properties, which will display validation messages on the click of Save button without data, as shown below in the image below.

Error Message

Blog Class

In the above image, we created a class named Blog with data validation through data Annotation.

DataAnnotations

ASP.NET MVC uses DataAnnotations attributes to implement validations. DataAnnotations includes built-in validation attributes for different validation rules, which can be applied to the properties of model class. ASP.NET MVC framework will automatically enforce these validation rules and display validation messages in the view.
The DataAnnotations attributes included in System.ComponentModel.DataAnnotations namespace. The following table lists DataAnnotations validation attributes.
Attribute
Description
Required
Indicates that the property is a required field
StringLength
Defines maximum length for string field
Range
Defines minimum and maximum value for a numeric field
RegularExpression
Speicifies the field value must match with speicified Regular expression
CreditCard
Speci
Specifies that the specified field is a credit card number
CustomValidation
Specifies custom validation method to validate the field
EmailAddress
Validates with email address format
FileExtension
Validates with File extension
MaxLength
Specifies maximum length for a string field
MinLength
Sepcifies minimum length for a string field
Phone
Specifies that the field is  a phone number using regular expression for phone number

Implementing Validation to our Blog Class

Apply DataAnnotation attributes to the properties of our Blog class as shown below


Here, we have made the Title property of the blog class as compulsory by decorating it with Required attribute with error to be displayed in case of irregularities.

The DatePosted property is decorated with two attributes to make it compulsory and display in Calendar Date picker as shown below

Date Error

Save the project and create a controller with scaffolding to generate the Create and Edit methods and View pages as shown below
Controller

Run the application and see that data validation is properly enforced by the system as can be seen below
Edit Error

The view page provides the ValidationMessage HTML helper method for error messages display as can be seen below
Validation Message

Thank you  



Comments

Post a Comment

Popular posts from this blog

Classes in C# Explained

C# Class Explained A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity, as explained by Guru99 . For instance, if you want to work with Guest’s data as in our previous DataDriven Web application . The properties of the Guest would be the Id, GuestName, Address, Phone number etc of the Guest. The methods would include the entry and modification of Guest data. All of these operations can be represented as a class in C# as shown below. using System; namespace CsharpnaijaClassTutorial {     public class Guest     {         public int Id { get ; set ; }         public string GuestName { get ; set ; }         public string Address { get ; set ; }         public string WhomToSee { get ; set ; }     ...

ASP.NET MVC Views

Views in ASP.NET MVC Application explained Find a related article By  Steve Smith  and  Luke Latham from Microsoft Corporation here In the Model-View-Controller (MVC) pattern, the  view  handles the application's data presentation and user interaction. A view is an HTML template with embedded  Razor markup . Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET MVC, views are  .cshtml  files that use the  C# programming language  in Razor markup. Usually, view files are grouped into folders named for each of the application's  controllers . The folders are stored in a  Views  folder at the root of the application as shown: The  Home  controller is represented by a  Home  folder inside the  Views  folder.  The  Home  folder contains the views for the  About ,  Contact , and  Index...

ASP.NET MVC Routing

ASP.NET MVC Routing ASP.NET MVC routing is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When the ASP.NET MVC application launches then the application registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that matches those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match. Routing pattern is as follows A URL is requested from a browser, the URL is parsed (that is, break into controller and action), the parsed URL is compared to registered route pattern in the framework’s route table, if a route is found, its process and send response to the browser with the required response, otherwise, the HTTP 404 error is send to the browser. Route Properties ASP.NET MVC routes are res...