Exemplo n.º 1
0
        /// <summary>
        /// Provider registration helper, which replaces the existing
        /// <see cref="DataAnnotationsModelValidatorProvider"/> with the new one that
        /// supports .NET 4. If the old provider isn't registered, then the new one
        /// is added at the end of the list.
        /// </summary>
        /// <param name="addImpliedRequiredAttributes">A flag which indicates whether
        /// the validator provider should automatically generate <see cref="RequiredAttribute"/>
        /// instances for non-nullable value types when there isn't one already
        /// present. In <see cref="DataAnnotationsModelValidatorProvider"/>, this
        /// was always true.</param>
        public static void RegisterProvider(bool addImpliedRequiredAttributes = true)
        {
            // Start assuming we'll go at the end
            int index = ModelValidatorProviders.Providers.Count;

            // We need to remove the old DA provider, and record its place in the provider list
            // so we can preserve the original order
            var oldProvider = ModelValidatorProviders.Providers.FirstOrDefault(p => p is DataAnnotationsModelValidatorProvider);

            if (oldProvider != null)
            {
                index = ModelValidatorProviders.Providers.IndexOf(oldProvider);
                ModelValidatorProviders.Providers.Remove(oldProvider);
            }

            var newProvider = new DataAnnotations4ModelValidatorProvider {
                AddImpliedRequiredAttributes = addImpliedRequiredAttributes
            };

            ModelValidatorProviders.Providers.Insert(index, newProvider);
        }
        /// <summary>
        /// Provider registration helper, which replaces the existing
        /// <see cref="DataAnnotationsModelValidatorProvider"/> with the new one that
        /// supports .NET 4. If the old provider isn't registered, then the new one
        /// is added at the end of the list.
        /// </summary>
        /// <param name="addImpliedRequiredAttributes">A flag which indicates whether
        /// the validator provider should automatically generate <see cref="RequiredAttribute"/>
        /// instances for non-nullable value types when there isn't one already
        /// present. In <see cref="DataAnnotationsModelValidatorProvider"/>, this
        /// was always true.</param>
        public static void RegisterProvider(bool addImpliedRequiredAttributes = true) {
            // Start assuming we'll go at the end
            int index = ModelValidatorProviders.Providers.Count;

            // We need to remove the old DA provider, and record its place in the provider list
            // so we can preserve the original order
            var oldProvider = ModelValidatorProviders.Providers.FirstOrDefault(p => p is DataAnnotationsModelValidatorProvider);
            if (oldProvider != null) {
                index = ModelValidatorProviders.Providers.IndexOf(oldProvider);
                ModelValidatorProviders.Providers.Remove(oldProvider);
            }

            var newProvider = new DataAnnotations4ModelValidatorProvider { AddImpliedRequiredAttributes = addImpliedRequiredAttributes };
            ModelValidatorProviders.Providers.Insert(index, newProvider);
        }