ASP.NET MVC Hosting :: Data Annotations For MVC in ASP.NET

Today I will explain about data anntations for MVC in ASP.NET.

Do you know Data Annotations?

Data Annotations is a library in the .NET Framework that helps in validation. The developer doesn’t need to create the logic for the validation since the validation can be done by specifying the data annotation attributes that forces the data validation rules.

ahp banner aspnet mvc-01

Namespace

The namespace System.ComponentModel.DataAnnotations provides the attribute classes for validations.

Attribute Classes

There are more than 30 attribute classes in the System.ComponentModel.DataAnnotations namespace. We will discuss some of the most commonly used attributes.

  • RequiredAttribute
  • Range Attribute
  • EmailAddressAttribute
  • MinLengthAttribute
  • MaxLengthAttribute

Required Attribute

The Required Attribute forces the required field validation for the specified data field.
Example

The Error message property is used to get/set the error message.

Range Attribute

The Range attribute forces the range validation for the data field.
Example

EmailAddressAttribute

The Email address attribute forces the validation and checks to ensure that the field has a valid email ID.
Example

Regular Expression Attribute

Regular expressions can be added to a data field. The added regular expression will be forced for validation.

Some of most commonly used snippets for validation

 


Used for checking the field to have only numbers and min of 8 digits and max of 11 digits.

Used for checking the field to have only numbers with 2 decimal places.

Used for validation Zip code. The Zip code should contain 5 digits and should not be 00000.

The field should contain 4 digit numbers. Used for limiting the characters and allowing only numbers.

The field should contain a minimum of 5 digit numbers and a maximum of 9 digit numbers.
Regex for validating Email.

Used while validating the Zip codes. Also the number should not start with “0”.

MinLengthAttribute

Validates the minimum length of the data field (String/Array). It will not work for int types.

MaxLengthAttribute

Validates the maximum length of the data field (String/Array). It will not work for int types.

Enabling Unobtrusive Client-side Validation via data Annotaions

When we include the following changes in the config file, the client-side validation will be done based on the data annotation rules that we have defined in the model.

The following scripts should be added to the view page to enable Unobtrusive Client-side Validation via data Annotations.

 

No #1 Recommended ASP.NET MVC Hosting

ASPHostPortal.com

ASPHostPortal.com  is the leading provider of Windows hosting and affordable ASP.NET  MVC Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.

HostForLIFE.eu

HostForLIFE.eu guarantees 99.9% uptime for their professional ASP.NET MVC hosting and actually implements the guarantee in practice. HostForLIFE.eu is the service are excellent and the features of the web hosting plan are even greater than many hosting. HostForLIFE.eu offer IT professionals more advanced features and the latest technology. Relibility, Stability and Performance of  servers remain and TOP priority. Even basic service plans are equipped with standard service level agreements for 99.99% uptime. Advanced options raise the bar to 99.99%. HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools.

DiscountService.com.au

DiscountService.com.au is The Best and Cheap ASP.NET MVC Hosting. DiscountService.com.au was established to cater to an under served market in the hosting industry web hosting for customers who want excellent service. DiscountService.com.au guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability. DiscountService.com.au has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch. DiscountService.com.au is devoted to offering the best Windows hosting solution for you.

Leave a Reply

Your email address will not be published.