Sunday, 23 December 2018

Useful attributes in MVC


  • StringLength Attribute :
Used to enforce minimum and maximum length of characters that are allowed in a data field. Let's understand this with an example. 

Namespace: 

System.ComponentModel.DataAnnotations

    [MetadataType(typeof(EmployeeMetaData))]
    public partial class Employee
    {
 
    }
 
    public class EmployeeMetaData
    {
        [StringLength (10,MinimumLength =5)]
        public string Name { getset; }
    }

  • Range Attribute
Range attribute is used to validate DateTime fields, numeric fields.
    [MetadataType(typeof(EmployeeMetaData))]
    public partial class Employee
    {
 
    }
 
    public class EmployeeMetaData
    {
        [StringLength (10,MinimumLength =5)]
        public string Name { getset; }
 
        [Range(1,100)]
        public string Age { getset; }

        [Range(typeof(DateTime),"01/01/2010","01/01/2018")]
        public DateTime? DateofJoin { getset; }
    }

  • Regular Expression Attribute
Regular expression attribute is used for pattern matching validation.
   [MetadataType(typeof(EmployeeMetaData))]
   public partial class Employee
   {
 
   }
 
   public class EmployeeMetaData
   {
       [RegularExpression(@"^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$")]
       [StringLength (10,MinimumLength =5)]
       public string Name { getset; }
   }

Or

        [RegularExpression(@"^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$",ErrorMessage = "Only upper and lower case alphabets are allowed")]
        [StringLength (10,MinimumLength =5)]
        public string Name { getset; }
   
  • Compare Attribute
Compare attribute is used to compare 2 properties of a model. 
   [MetadataType(typeof(EmployeeMetaData))]
   public partial class Employee
   {
     [Compare("Name")]
     public string confirmName { getset; }
   }
 
   public class EmployeeMetaData
   {
       [RegularExpression(@"^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$",ErrorMessage = "Only upper and lower case alphabets are allowed")]
       [StringLength (10,MinimumLength =5)]
 
       public string Name { getset; }
   }

  • Required Attribute
This is used to enforce the property is required 
    [MetadataType(typeof(EmployeeMetaData))]
    public partial class Employee
    {
      
    }
 
    public class EmployeeMetaData
    {
        [Required]
        public string Name { getset; }
 
        [Required]
        public string Designation { getset; }
    }

  • Display Attribute
This is used to display change the caption (Overrides auto generated name)
[MetadataType(typeof(DepartmentMetaData))] public partial class Department { } public class DepartmentMetaData {     [Display(Name="Department Name")]     public string Name { get; set; } }
  • DisplayName Attribute
Namespace: using System.ComponentModel;

        [DisplayName("Employee Name")]
        public string Name { getset; }

  • DispalyAttribute Attribute
       [DisplayAttribute(Name="Employee Name")]
       public string Name { getset; }

  • DisplayFormat Attribute
 If gender is NULL, "Gender not specified" text will be displayed.

    [DisplayFormat(NullDisplayText = "Gender not specified")]
        public string Gender { get; set; }

  • ScaffoldColumn Attribute
If you don't want to display a column use ScaffoldColumn attribute. This only works when you use @Html.DisplayForModel() helper
        [ScaffoldColumn(false)]         public int? Salary { get; set; }

  • datatype Attribute

Used to specify specific data type in the model.
    [DataType(DataType.EmailAddress)]     public string EmailAddress { get; set; }
    [DataType(DataType.Currency)]     public int? Salary { get; set; }
    [DataType(DataType.Url)]     public string PersonalWebSite { get; set; }
    [DataType(DataType.Date)]     public DateTime? HireDate { get; set; }

No comments:

Post a Comment