Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected bool IsValidModel(object bindingModel)
- {
- foreach (var property in bindingModel.GetType().GetProperties())
- {
- IEnumerable<Attribute> attributes = property
- .GetCustomAttributes()
- .Where(a => a is PropertyAttribute);
- foreach (PropertyAttribute attribute in attributes)
- {
- if (!attribute.IsValid(property.GetValue(bindingModel)))
- {
- return false;
- }
- }
- IEnumerable<Attribute> validationAttributes = property
- .GetCustomAttributes()
- .Where(a => a is ValidationAttribute);
- foreach (ValidationAttribute validationAttribute in validationAttributes)
- {
- if (!validationAttribute.IsValid(property.GetValue(bindingModel)))
- {
- this.ShowAlert(validationAttribute.FormatErrorMessage(property.Name), "danger");
- return false;
- }
- }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment