Exemple #1
0
        protected internal bool TryValidateModel(object model, string prefix)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(
                () => model,
                model.GetType()
                );

            foreach (
                ModelValidationResult validationResult in ModelValidator
                .GetModelValidator(metadata, ControllerContext)
                .Validate(null)
                )
            {
                ModelState.AddModelError(
                    DefaultModelBinder.CreateSubPropertyName(prefix, validationResult.MemberName),
                    validationResult.Message
                    );
            }

            return(ModelState.IsValid);
        }
Exemple #2
0
            public override IEnumerable <ModelValidationResult> Validate(object container)
            {
                bool propertiesValid = true;

                foreach (ModelMetadata propertyMetadata in Metadata.Properties)
                {
                    foreach (ModelValidator propertyValidator in propertyMetadata.GetValidators(ControllerContext))
                    {
                        foreach (ModelValidationResult propertyResult in propertyValidator.Validate(Metadata.Model))
                        {
                            propertiesValid = false;
                            yield return(new ModelValidationResult
                            {
                                MemberName = DefaultModelBinder.CreateSubPropertyName(propertyMetadata.PropertyName, propertyResult.MemberName),
                                Message = propertyResult.Message
                            });
                        }
                    }
                }

                if (propertiesValid)
                {
                    foreach (ModelValidator typeValidator in Metadata.GetValidators(ControllerContext))
                    {
                        foreach (ModelValidationResult typeResult in typeValidator.Validate(container))
                        {
                            yield return(typeResult);
                        }
                    }
                }
            }
 private static ModelValidationResult CreateSubPropertyResult(ModelMetadata propertyMetadata, ModelValidationResult propertyResult)
 {
     return(new ModelValidationResult
     {
         MemberName = DefaultModelBinder.CreateSubPropertyName(propertyMetadata.PropertyName, propertyResult.MemberName),
         Message = propertyResult.Message
     });
 }