jeudi 23 mars 2017

MVC Model Validation Mechanism

When we create a model (for example using EF Code First) using Data Annotations we can use Required, 'Range', ...:

public class Employee
{
   public int EmpId { get; set; }

   [DisplayName("Employee Name")]
   [Required(ErrorMessage = "Employee Name is required")]
   [StringLength(100,MinimumLength=3)]
   public String EmpName { get; set; } 

   [Required(ErrorMessage = "Employee Address is required")] 
   [StringLength(300)]
   public string Address { get; set; } 

   [Required(ErrorMessage = "Salary is required")] 
   [Range(3000, 10000000,ErrorMessage = "Salary must be between 3000 and 10000000")]
   public int Salary{ get; set; } 
}

and when using Html.ValidationSummary and Html.ValidationMessage we can show error messages.

My question is when a View would load, in specific page load state those validations will get and convert to JavaScript codes and inject to page. I want to know this mechanism and convert those Data Annotations to JavaScript. I want to create custom validations for models and I want to simulate that behavior.

Thanks

Aucun commentaire:

Enregistrer un commentaire