本文介绍了自定义验证属​​性不叫ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我已创建自定义的验证属性,并将其分配给类级别的验证。不幸的是,它不被调用。我尝试,它认为它可能是解决问题的各种方式。然而,它把我几个小时,我无法找到该属性没有被验证机制调用。

Hello everyone I have create custom validation attribute and assign it to class level validation. Unfortunately, it is not called. I try every way that it think it could be solve the problem. However, it take me for hours and I can't find the attribute is not called by validation mechanism.

有关说明你,我把下面的code。

For illustrate you I put the following code.

属性


[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class BooleanDependencyAttribute : ValidationAttribute
    {
        private const string _defaultErrorMessage = "กรุณากรอก{0}";
        private readonly object _typeId = new object();

        public string DependencyPropertyName { get; private set; }
        public string DependentPropertyName { get; private set; }

        public BooleanDependencyAttribute(string dependencyPropertyName, string dependentPropertyName)
            : base(_defaultErrorMessage)
        {
            DependencyPropertyName = dependencyPropertyName;
            DependentPropertyName = dependentPropertyName;
        }



        public override object TypeId
        {
            get
            {
                return _typeId;
            }
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,name);
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            bool dependencyValue = (bool) properties.Find(DependencyPropertyName, true /* ignoreCase */).GetValue(value);
            object dependentValue = properties.Find(DependentPropertyName, true /* ignoreCase */).GetValue(value);
            if (dependencyValue)
            {
                return true;
            }
            else
            {
                if (dependentValue == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

        }
    }

视图模型


  [BooleanDependency("ReleaseNow","ReleaseDate",ErrorMessage="Please enter release date")]
    public class ContentCreate
    {

        public string Title { get; set; }

        public DateTime? ReleaseDate { get; set; }


        public string Details { get; set; }

        public string Abstract { get; set; }

        public string Tags { get; set; }

        public bool ReleaseNow { get; set; }

    }

请你能不能帮我解决这个问题。

Please could you help me to solve this problem.

推荐答案

我找到了解决办法。事实上,在一流水平的验证后,所有属性级别的验证是有效的被调用。因此,我需要完成其他所需的属性然后BooleanDependencyAttribute将称为有效的数值。

I found the solution. In fact validation in class level is called after all property-level validations are valid. Therefore I need to complete other required property then BooleanDependencyAttribute will called and valid value.

感谢您查看,编辑标题和标签。

Thanks for view, edit the title and tag.

这篇关于自定义验证属​​性不叫ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:19
查看更多