ASP.NET MVC Validation with Data Anotations PDF

Title ASP.NET MVC Validation with Data Anotations
Course Application Development
Institution Durban University of Technology
Pages 4
File Size 374 KB
File Type PDF
Total Downloads 87
Total Views 146

Summary

App Dev - Tutorial...


Description

IT Dept. APPDEV2A/DS2

Introduction - ASP.NET MVC Model Validation using Data Annotations In ASP.NET MVC, there are several ways to validate the model data prior to saving the data into the data store.

1. Validate the model data explicitly 2. Implement the IValidateableObject interface 3. Specify Data Annotations [Recommended] Data Annotation is recommended because there are built-in Data Annotations in .NET Framework. You don’t have to implement your own validation logic, instead you specify the Validation Data Annotation that you need. The Data Annotations specified support both server-side & client-side validation. Data annotations are attribute classes that live in the System.ComponentModel.DataAnnotations namespace that you can use to apply to (decorate) classes or properties to enforce pre-defined validation rules and displaying suitable messages to end users. These attributes define common validation patterns, such as range checking and required fields. The beauty of using Data Annotation feature is that by applying these attributes, we can manage the data definition in a single place (i.e. model) and do not need re-write the same rules in multiple places. The Data Annotation attributes falls into three categories: 1. Validation Attributes: Used to enforce validation rules. 2. Display Attributes: Used to specify how data from a class /member is displayed in the UI. 3. Modelling Attributes: Used to specify the intended use of class member and the relationship between classes. Built-in Data Annotations Namespace: System.ComponentModel.DataAnnotations Validation Attribute CompareAttribute CustomValidationAttribute DataTypeAttribute MaxLengthAttribute MinLengthAttribute RangeAttribute RegularExpressionAttribut e RequiredAttribute StringLengthAttribute

Description Compares two properties Specifies a custom validation method Data type of the data field Max. length of array or string data allowed Min. length of array or string data allowed Numeric range constraints for the data field value Data field value must match the specified regular expression Data field value is required Min. and max. length of characters that are allowed in a data

IT Dept. APPDEV2A/DS2

field Although having this set of built in validations is great, it is a limited set. So in case the builtin cannot fulfil your requirements, you can also implement your own Data Annotation. All you do is derive from any of the inheritable (non sealed) attribute classes in the namespace, then code your business rules in the derived class methods. Annotations live in one place – the model, rather than a code behind, web page, controller, or view. This helps prevent data validation code from turning into Spaghetti scattered about your project. Note Before we take a look at some of the examples for the validation attributes that appear on the above table, it is important to add reference to System.ComponentModel.DataAnnotations assembly and the highlighted namespace in our domain model in order to utilize the built-in attributes for our validation.

IT Dept. APPDEV2A/DS2

Using validation attributes examples We will force the user to give us the product name, we can decorate the productName properties of the ProductModel model with required attributes. We can configure a default user friendly error message with ErrorMessage named attribute as shown below. Furthermore, we can decide to use Display attribute to set a friendly label name for our property (productName) model. [Required(ErrorMessage = "Please enter product name")] [DisplayName("Product Name")] public string productName { get; set; } If the productName property value is null or empty, the user will see the default error as shown below and the label will look as it appears below instead of showing productName :

We are forcing the user to enter the product name but what happens if he enters a name with enormous length? For product name, we will set maximum 60 characters that can be entered. Hence, in order to do that, we must decorate productName with StringLength attribute. [Required(ErrorMessage = "Please enter product name")] [DisplayName("Product Name")] [StringLength(60, MinimumLength = 2)] public string productName { get; set; } If the productName property has more than 60 characters, we will get an error message, as shown below:

IT Dept. APPDEV2A/DS2

Now suppose we are interested in stating that the cheapest product that we may have can cost as low as R60 and the expensive product may cost up R2500, which validation attribute can support this? It is a Range attributes. This validation attribute specifies the minimum and maximum constrains for a numerical number. Here, the first parameter of the attribute is the minimum value and second part is the maximum value. The Range attributes will work with an integer, double and another overloaded versions will take Type as parameters as shown below: [Required(ErrorMessage = "Please enter product price")] [DisplayName("Product Price")] [Range(typeof(double),"60,00","2500,00")] public double price { get; set; } In this example, the price of the product will be between 60 to 25000 to pass the validation. Otherwise, the error message like the one below will show.

Thanks for your time. I hope this introduction was informative and helpful. Please check out my next posts, where I’ll be binding the model (specifically looking at the validation aspect) with the view using the same example. Also, I’ll touch a bit on controllers. Happy Coding!!...


Similar Free PDFs