代码之家  ›  专栏  ›  技术社区  ›  KKS

如何访问自定义验证属性中视图模型的属性值以更改消息?

  •  5
  • KKS  · 技术社区  · 12 年前

    视图模型有许多字符串财产,如 Sample 如下所示。我的要求是根据视图模型中的bool标志显示不同的验证消息。那面旗帜是 IsProposer 属性如下所述:

    [SampleAttribute(true, "bla prop", "foo add driver")]       
    public string Sample { get; set; }
    
    public bool IsProposer { get; set; }
    

    我想创建一个验证属性,这样我就可以把它放在所有字符串财产上(需要验证)。然后,根据布尔标志的值,我将相应地传递消息。我的自定义验证属性如下:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
        public class SampleAttribute : RequiredAttribute
        {
            protected string ProposerErrorMessage { get; set; }
            protected string AdditionalDriverErrorMessage { get; set; }
            protected bool IsProposer { get; set; }
            public SampleAttribute(bool isProposer, string propmsg, string adddrivermsg)
            {
                ProposerErrorMessage = propmsg;
                IsProposer = isProposer;
                AdditionalDriverErrorMessage = adddrivermsg;
    
            }
    
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                if (IsValid(value))
                {
                    return ValidationResult.Success;
                }
                else
                {
                    return new ValidationResult(IsProposer ? ProposerErrorMessage : AdditionalDriverErrorMessage);
                }
            }
        }
    

    现在的问题是,正如您所看到的,我只是传递true作为属性的第一个参数。给,我需要通过 Isproposer 属性的值,这样我就可以相应地采取行动。我如何访问它?

    1 回复  |  直到 12 年前
        1
  •  11
  •   KKS    12 年前

    我通过创建这样的属性解决了我的问题:

     /// <summary>
        /// This validation attribute is an extension to RequiredAttribute that can be used to choose either of the two 
        /// validation messages depending on a property in the context of same model.
        /// </summary>
        [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
        public class RequiredExtensionAttribute : RequiredAttribute
        {
            private string _errorMessageIfTruthy;
            private string _errorMessageIfFalsy; 
            private string _dependentProperty;
    
            public RequiredExtensionAttribute(string dependentproperty, string errorMessageIfTruthy, string errorMessageIfFalsy)
            {
                _errorMessageIfTruthy = errorMessageIfTruthy;
                _dependentProperty = dependentproperty;
                _errorMessageIfFalsy = errorMessageIfFalsy;
    
            }
    
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                var propertyTestedInfo = validationContext.ObjectType.GetProperty(this._dependentProperty);
                if (propertyTestedInfo == null)
                {
                    return new ValidationResult(string.Format("unknown property {0}", this._dependentProperty));
                }
    
                var propertyTestedValue = propertyTestedInfo.GetValue(validationContext.ObjectInstance, null);
    
                if (IsValid(value))
                {
                    return ValidationResult.Success;
                }
                else
                {
                    return new ValidationResult((bool)propertyTestedValue ? _errorMessageIfTruthy : _errorMessageIfFalsy);
                }
            }
        }
    

    现在可以在以下模型中使用:

    [RequiredExtensionAttribute("IsProposerViewModel", "Please select your employment status.", "Please select this driver's employment status")]       
    public string EmploymentStatus { get; set; }
    public bool IsProposerViewModel { get; set; }
    

    -其中属性的第一个参数是 IsProposerViewModel ,依赖值。