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);
        }
        protected virtual void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            Dictionary <string, bool> startedValid = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);

            foreach (ModelValidationResult validationResult in ModelValidator.GetModelValidator(bindingContext.ModelMetadata, controllerContext).Validate(null))
            {
                string subPropertyName = CreateSubPropertyName(bindingContext.ModelName, validationResult.MemberName);

                if (!startedValid.ContainsKey(subPropertyName))
                {
                    startedValid[subPropertyName] = bindingContext.ModelState.IsValidField(subPropertyName);
                }

                if (startedValid[subPropertyName])
                {
                    bindingContext.ModelState.AddModelError(subPropertyName, validationResult.Message);
                }
            }
        }