代码之家  ›  专栏  ›  技术社区  ›  Himanshu kamothi

需要如果需要检查数据数据注释所需的验证

  •  -1
  • Himanshu kamothi  · 技术社区  · 8 年前

    如何放置条件数据注释? 当所需的属性值依赖于其他属性值时,如何准备数据注释验证?

    我已编码如下:

    [RequiredIf("property_name==\"property_Value\"", ErrorMessageResourceType = typeof(Resources.resfilename), ErrorMessageResourceName = "ErrMessage")]
            public int? propertyname { get; set; }
    

    如何生成required if类?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Himanshu kamothi    8 年前

    创建一个类,如下所示:

    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    
    namespace BusinessModels
    {
        public class RequiredIfAttribute : ValidationAttribute, IClientValidatable
        {
            private readonly string _condition;
    
            public RequiredIfAttribute(string condition)
            {
                _condition = condition;
            }
    
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                Delegate conditionFunction = CreateExpressionDelegate(validationContext.ObjectType, _condition);
    
                bool conditionMet = (bool) conditionFunction.DynamicInvoke(validationContext.ObjectInstance);
    
                if (conditionMet)
                {
                    if (value == null)
                    {
                        return new ValidationResult(FormatErrorMessage(null));
                    }
                }
                return null;
            }
    
            private Delegate CreateExpressionDelegate(Type objectType, string expression)
            {
                // TODO - add caching
                var lambdaExpression = CreateExpression(objectType, expression);
                Delegate func = lambdaExpression.Compile();
                return func;
            }
    
            private LambdaExpression CreateExpression(Type objectType, string expression)
            {
                // TODO - add caching
                LambdaExpression lambdaExpression =
                    System.Linq.Dynamic.DynamicExpression.ParseLambda(
                        objectType, typeof(bool), expression);
                return lambdaExpression;
            }
    
            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                string errorMessage = FormatErrorMessage(metadata.GetDisplayName());
    
                Expression expression = CreateExpression(metadata.ContainerType, _condition);
                var visitor = new JavascriptExpressionVisitor();
                string javascriptExpression = visitor.Translate(expression);
    
                var clientRule = new ModelClientValidationRule
                {
                    ErrorMessage = errorMessage,
                    ValidationType = "requiredif",
                    ValidationParameters =
                                             {
                                                 { "expression", javascriptExpression }
                                             }
                };
    
                return new[] { clientRule };
            }
    
        }
    }
    

    并调用如下属性:

    [RequiredIf("property_name==\"prop_value\"", ErrorMessageResourceType = typeof(Resources.resfilename), ErrorMessageResourceName = "ErrMessage")]
            public int? propertyname { get; set; }