3
大家好我已经创建了自定义验证属性并将其分配给类级验证。不幸的是,它没有被调用。我想尽一切办法认为它可以解决问题。然而,它花了我几个小时,我无法找到该属性不被验证机制调用。不调用自定义验证属性ASP.NET MVC
为了说明你,我把下面的代码。
属性
[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; } }
请你能不能帮我解决这个问题。