当前位置: 首页>>代码示例>>C#>>正文


C# ModelMetadata.GetValidators方法代码示例

本文整理汇总了C#中ModelMetadata.GetValidators方法的典型用法代码示例。如果您正苦于以下问题:C# ModelMetadata.GetValidators方法的具体用法?C# ModelMetadata.GetValidators怎么用?C# ModelMetadata.GetValidators使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ModelMetadata的用法示例。


在下文中一共展示了ModelMetadata.GetValidators方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetValidators_should_return_validators_for_item_in_list_and_validators_for_itself

        public void GetValidators_should_return_validators_for_item_in_list_and_validators_for_itself()
        {
            // Arrange
            var model = new ParentClass
            {
                Children = new List<NestedClass>
                {
                    new NestedClass(),
                    new NestedClass()
                }
            };

            // Action
            var metaData = new ModelMetadata(ModelMetadataProviders.Current,
                                             typeof(ParentClass),
                                             () => model.Children,
                                             typeof(List<NestedClass>),
                                             "Children");

            var validators = metaData.GetValidators().ToList();

            // Assert
            Assert.AreEqual(3, validators.Count());
        }
开发者ID:Chunhui-Liu,项目名称:Blog,代码行数:24,代码来源:ModelMetadataTests.cs

示例2: LoadMaxLength

        private static void LoadMaxLength(HtmlHelper html, ModelMetadata metadata, IDictionary<string, object> editorAttributes)
        {
            Throw.IfNullArgument(editorAttributes, "editorAttributes");

            var maxlength = -1;
            var validators = metadata.GetValidators(html.ViewContext.Controller.ControllerContext);
            var lengthLimits = validators.OfType<StringLengthAttributeAdapter>();
            foreach (var limit in lengthLimits)
            {
                foreach (var rule in limit.GetClientValidationRules())
                {
                    var max = rule.ValidationParameters.Get("max", -1);
                    if (max != -1)
                    {
                        if (maxlength == -1 || max < maxlength)
                            maxlength = max;
                    }
                }
            }

            if (maxlength != -1 && !editorAttributes.ContainsKey("maxlength"))
                editorAttributes.Add("maxlength", maxlength);
        }
开发者ID:kamaelyoung,项目名称:Carcass,代码行数:23,代码来源:EditorTemplates.cs

示例3: CreateModelFieldFromMeta

        public static ModelField CreateModelFieldFromMeta(ModelMetadata propertyMetadata, Ext.Net.Model storeModel = null, ModelFieldAttribute modelAttr = null, ControllerContext controllerContext = null)
        {
            var modelField = new ModelField();
            if (modelAttr == null)
            {
                modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null;
            }           

            modelField.Name = propertyMetadata.PropertyName;

            if (Store.IsDate(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Date;
            }
            else if (Store.IsInteger(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Int;
            }
            else if (Store.IsFloat(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Float;
            }
            else if (Store.IsBoolean(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Boolean;
            }
            else if (propertyMetadata.ModelType == typeof(string))
            {
                modelField.Type = ModelFieldType.String;
            }

            if (propertyMetadata.IsComplexType && modelAttr != null)
            {
                modelField.IsComplex = true;
            }

            if (modelAttr != null && storeModel != null)
            {
                modelAttr.CopyTo(modelField, storeModel);
            }

            if(storeModel != null)
            {
                storeModel.Fields.Add(modelField);
            }

            ValidationCollection validations = new ValidationCollection();
            if (propertyMetadata.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY))
            {
                validations = (ValidationCollection)propertyMetadata.AdditionalValues[AbstractValidationAttribute.KEY];                
            }

            foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext))
            {
                var rule = v.GetClientValidationRules().FirstOrDefault();

                if (rule != null)
                {
                    switch (rule.ValidationType)
                    {
                        case "required":
                            validations.Add(new PresenceValidation { Message = rule.ErrorMessage });
                            break;
                        case "regex":
                            if (rule.ValidationParameters.ContainsKey("pattern"))
                            {
                                validations.Add(new FormatValidation { Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage });
                            }
                            break;                        
                        case "length":
                            int num;
                            LengthValidation length = new LengthValidation();
                            length.Message = rule.ErrorMessage;
                            if (rule.ValidationParameters.ContainsKey("max"))
                            {
                                if (int.TryParse(rule.ValidationParameters["max"].ToString(), out num))
                                {
                                    length.Max = num;
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            if (rule.ValidationParameters.ContainsKey("min"))
                            {
                                if (int.TryParse(rule.ValidationParameters["min"].ToString(), out num))
                                {
                                    length.Min = num;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            validations.Add(length);
                            break;
                    }
                }
//.........这里部分代码省略.........
开发者ID:hh22333,项目名称:Ext.NET.Community,代码行数:101,代码来源:Store.cs

示例4: ApplyFieldValidationMetadata

 private static FieldValidationMetadata ApplyFieldValidationMetadata(HtmlHelper htmlHelper, ModelMetadata modelMetadata, string modelName)
 {
     FieldValidationMetadata validationMetadataForField = htmlHelper.ViewContext.FormContext.GetValidationMetadataForField(modelName, true);
     foreach (IEnumerable<ModelClientValidationRule> rules in
         (from v in modelMetadata.GetValidators(htmlHelper.ViewContext.Controller.ControllerContext) select v.GetClientValidationRules()))
     {
         foreach (var rule in rules)
         {
             validationMetadataForField.ValidationRules.Add(rule);
         }
     }
     return validationMetadataForField;
 }
开发者ID:Godoy,项目名称:CMS,代码行数:13,代码来源:ValidationExtensions.cs


注:本文中的ModelMetadata.GetValidators方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。