Asp.net mvc 使用EF buddy类上的自定义属性(AttributeTargets.Class)进行数据验证
我有一个实体框架生成的类,具有以下属性:Asp.net mvc 使用EF buddy类上的自定义属性(AttributeTargets.Class)进行数据验证,asp.net-mvc,entity-framework,data-annotations,Asp.net Mvc,Entity Framework,Data Annotations,我有一个实体框架生成的类,具有以下属性: public DateTime LaunchDate; public DateTime ExpirationDate; 我需要强制执行ExpirationDate>LaunchDate 我正在使用各种帖子中描述的buddy类。 我正在将自定义验证属性(在属性上)应用于同一buddy类中的其他属性,这些属性正在工作 因为我需要比较两个属性,所以我直接在类(AttributeTargets.class)上使用一个属性 以下是我的自定义验证属性: [Attr
public DateTime LaunchDate;
public DateTime ExpirationDate;
我需要强制执行ExpirationDate>LaunchDate
我正在使用各种帖子中描述的buddy类。
我正在将自定义验证属性(在属性上)应用于同一buddy类中的其他属性,这些属性正在工作
因为我需要比较两个属性,所以我直接在类(AttributeTargets.class)上使用一个属性
以下是我的自定义验证属性:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class PropertyMustBeGreaterThanAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{0}' must be greater than '{1}'!";
public PropertyMustBeGreaterThanAttribute(string property, string greaterThan)
: base(_defaultErrorMessage)
{
GreaterThan = greaterThan;
Property = property;
}
public string Property { get; private set; }
public string GreaterThan { get; private set; }
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
GreaterThan, Property);
}
public override bool IsValid(object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
IComparable greaterThan = properties.Find(GreaterThan, true /* ignoreCase */).GetValue(value) as IComparable;
IComparable property = properties.Find(Property, true /* ignoreCase */).GetValue(value) as IComparable;
return greaterThan.CompareTo(property) > 0;
}
}
首先,我不确定需要将属性应用于哪个类:
[MetadataType(typeof(PromotionValidation))]
[PropertyMustBeGreaterThanAttribute("RetailPrice")] // apply it here
public partial class Promotion
{
[PropertyMustBeGreaterThanAttribute("ExpirationDate", "LaunchDate")] // or here ???
public class PromotionValidation
{
第二,它不工作,我不知道为什么!!!
我已经尝试向这两个类添加属性。构造函数中的断点被命中(PropertyMustbegreaterNaAttribute),但从未调用IsValid
已经把我的头发拔出来了
谢谢 通过实现TypeID实现了它。 我将属性指定给分部类:
[MetadataType(typeof(PromotionValidation))]
[PropertyMustBeGreaterThanAttribute("RetailPrice", "DiscountedPrice")]
[PropertyMustBeGreaterThanAttribute("ExpirationDate", "LaunchDate")]
[PropertyMustBeGreaterThanAttribute("ClaimDeadline", "ExpirationDate")]
public partial class Promotion
{
...
以下是清单,以备任何人需要:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class PropertyMustBeGreaterThanAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{1}' must be greater than '{0}'!";
private readonly object _typeId = new object();
public PropertyMustBeGreaterThanAttribute(string property, string greaterThan)
: base(_defaultErrorMessage)
{
GreaterThan = greaterThan;
Property = property;
}
public string Property { get; private set; }
public string GreaterThan { get; private set; }
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
GreaterThan, Property);
}
public override bool IsValid(object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
IComparable greaterThan = properties.Find(GreaterThan, true /* ignoreCase */).GetValue(value) as IComparable;
IComparable property = properties.Find(Property, true /* ignoreCase */).GetValue(value) as IComparable;
return greaterThan.CompareTo(property) > 0;
}
public override object TypeId
{
get
{
return _typeId;
}
}
}
我尝试了代码,但当我尝试调用Validator.TryValidateObject时,它没有进入“bool IsValid(object value)”方法。我怎样才能做到这一点?@LawrenceA.Contreras我已经很久没有使用asp.net mvc了。我现在使用node.js,但无论如何,我确实记得这个问题,而且我确实记得它按预期工作。我检查了上面发布的代码,它是完整的。我从未直接调用过验证,因为验证是作为数据验证框架的一部分进行的。你看了吗?