示例#1
0
        public static void CheckErrors(object model, System.Web.Mvc.ModelStateDictionary modelState, bool validateNotUpdateedFields)
        {
            Dictionary <string, List <ValidationRule> > errorRules = ValidationCheck.GetValidationRules(model.GetType());

            foreach (KeyValuePair <string, List <ValidationRule> > item in errorRules)
            {
                object obj = model.GetType().InvokeMember(item.Key, BindingFlags.GetProperty, null, model, null);

                foreach (ValidationRule rule in item.Value)
                {
                    if (!rule.IFieldValidation.Validate(obj))
                    {
                        if (modelState.ContainsKey(rule.FieldName) || validateNotUpdateedFields)
                        {
                            modelState.AddModelError(rule.FieldName, rule.IFieldValidation.GetErrorMessage(rule.FieldTitle));
                        }
                        continue;
                    }
                }
            }
        }