Data Annotation In ASP.NET MVC

Data annotation are the attributes, which provide Server side validation and the framework also supports client side validation when we use one of the attributes on a model property. It resides in System.ComponentModel.DataAnnotations hierarchy. MVC supports the attributes given below.

  • Required
  • StringLength
  • RegularExpresson
  • Range
  • Compare
  • Remote

In Model class 

  1. [Required]  
  2. public string FirstName {  
  3.     get;  
  4.     set;  
  5. }  
  6. [StringLength(100, MinimumLength = 30)]  
  7. public string LastName {  
  8.     get;  
  9.     set;  
  10. }  
  11. [Compare(“Email”)]  
  12. Public string EmailConfirm {  
  13.     get;  
  14.     set;  
  15. }  
  16. [Remote(“CheckUserName”, ”Account”)]  
  17. Public string UserName {  
  18.     get;  
  19.     set;  
  20. }

Ebook Download
View all
Learn
View all