Danny_Berova

IsValidModel

Jun 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1.  protected bool IsValidModel(object bindingModel)
  2.         {
  3.             foreach (var property in bindingModel.GetType().GetProperties())
  4.             {
  5.                 IEnumerable<Attribute> attributes = property
  6.                     .GetCustomAttributes()
  7.                     .Where(a => a is PropertyAttribute);
  8.  
  9.                 foreach (PropertyAttribute attribute in attributes)
  10.                 {
  11.                     if (!attribute.IsValid(property.GetValue(bindingModel)))
  12.                     {
  13.                         return false;
  14.                     }
  15.                 }
  16.  
  17.                 IEnumerable<Attribute> validationAttributes = property
  18.                     .GetCustomAttributes()
  19.                     .Where(a => a is ValidationAttribute);
  20.  
  21.                 foreach (ValidationAttribute validationAttribute in validationAttributes)
  22.                 {
  23.                     if (!validationAttribute.IsValid(property.GetValue(bindingModel)))
  24.                     {
  25.                         this.ShowAlert(validationAttribute.FormatErrorMessage(property.Name), "danger");
  26.                         return false;
  27.                     }
  28.                 }
  29.             }
  30.  
  31.             return true;
  32.         }
Add Comment
Please, Sign In to add comment