【发布时间】:2011-04-04 22:04:20
【问题描述】:
大家好,我创建了自定义验证属性并将其分配给类级别验证。不幸的是,它没有被调用。我尝试了它认为可以解决问题的所有方法。但是,我花了几个小时,我找不到验证机制没有调用该属性。
为了说明,我放了以下代码。
属性
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] 公共密封类 BooleanDependencyAttribute : ValidationAttribute { private const string _defaultErrorMessage = "กรุณากรอก{0}"; 私有只读对象 _typeId = new object(); 公共字符串 DependencyPropertyName { 获取;私人套装; } 公共字符串 DependentPropertyName { 获取;私人套装; } 公共 BooleanDependencyAttribute(字符串依赖属性名称,字符串依赖属性名称) :基础(_defaultErrorMessage) { 依赖属性名 = 依赖属性名; 依赖属性名 = 依赖属性名; } 公共覆盖对象 TypeId { 得到 { 返回_typeId; } } 公共覆盖字符串FormatErrorMessage(字符串名称) { return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,name); } 公共覆盖布尔IsValid(对象值) { PropertyDescriptorCollection 属性 = TypeDescriptor.GetProperties(value); bool dependencyValue = (bool) properties.Find(DependencyPropertyName, true /* ignoreCase */).GetValue(value); objectdependentValue = properties.Find(DependentPropertyName, true /* ignoreCase */).GetValue(value); 如果(依赖值) { 返回真; } 别的 { if (dependentValue == null) { 返回假; } 别的 { 返回真; } } } }视图模型
[BooleanDependency("ReleaseNow","ReleaseDate",ErrorMessage="请输入发布日期")] 公共类内容创建 { 公共字符串标题 { 获取;放; } 公共日期时间?发布日期 { 获取;放; } 公共字符串详细信息 { 获取;放; } 公共字符串摘要 { 获取;放; } 公共字符串标签 { 获取;放; } 公共布尔 ReleaseNow { 获取;放; } }请你帮我解决这个问题。
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-2