Tutorial by Examples

Model using System.ComponentModel.DataAnnotations; public class ViewModel { [Required(ErrorMessage="Name is required")] public string Name { get; set; } [StringLength(14, MinimumLength = 14, ErrorMessage = "Invalid Phone Number")] [Required(ErrorMessag...
Remote Validation used to check whether the content enter in the input control is valid or not by sending an ajax request to server side to check it. Working The RemoteAttribute works by making an AJAX call from the client to a controller action with the value of the field being validated. The con...
The Required attribute specifies that a property is required. An error message can be specified on using the ErrorMessage property on the attribute. First add the namespace: using System.ComponentModel.DataAnnotations; And apply the attribute on a property. public class Product { [Require...
The StringLength attribute specifies the minimum and maximum length of characters that are allowed in a data field. This attribute can be applied on properties, public fields and parameters. The error message must be specified on the ErrorMessage property on the attribute. The properties MinimumLeng...
The Range attribute can decorate any properties or public fields and specifies a range that a numerical field must fall between to be considered valid. [Range(minimumValue, maximumValue)] public int Property { get; set; } Additionally, it accepts an optional ErrorMessage property that can be us...
The [RegularExpression] attribute can decorate any properties or public fields and specifies a regular expression that must be matched for the property be considered valid. [RegularExpression(validationExpression)] public string Property { get; set; } Additionally, it accepts an optional ErrorM...
The Compare attribute compares two properties of a model. The error message can be specified using property ErrorMessage, or using resource files. To use Compare attribute include using for the following namespace: using System.ComponentModel.DataAnnotations; Then you can use the attribute in ...
When it comes to validate some rules which are not generic data validation e.g ensuring a field is required or some range of values but they are specific to your business logic then you can create your own Custom Validator. To create a custom validation attribute, you just need to inherit Validation...
Edmx model internel public partial class ItemRequest { public int RequestId { get; set; } //... } Adding data annotation to this - if we modify this model directly, when a update to the model is made, the changes are lost . so To add a attribute in this case 'Required' Create a new...
[MetadataType(typeof(RoleMetaData))] public partial class ROLE { } public class RoleMetaData { [Display(Name = "Role")] public string ROLE_DESCRIPTION { get; set; } [Display(Name = "Username")] public string ROLE_USERNAME { get; set; } } If you us...

Page 1 of 1