小编Jac*_*ack的帖子

自定义数据注释永远不会调用IsValid.(ASP.NET MVC 2 .NET 4)

我有一个自定义数据验证属性我创建,以确保用户输入的密码是相同的,但从不调用IsValid.

自定义属性:

public class IsSameAsAttribute : ValidationAttribute
{
    public String TargetProperty { get; set; }
    private readonly object _typeId = new object();

    public IsSameAsAttribute(string targetProperty)
    {
        TargetProperty = targetProperty;
    }

    public override bool IsValid(object value)
    {
        return false;
        //Type objectType = value.GetType();
        //bool isValid = false;

        //PropertyInfo[] neededProperties =
        //    objectType.GetProperties().Where(propertyInfo => propertyInfo.Name == TargetProperty).ToArray();

        //return isValid;
    }

    public override object TypeId
    {
        get { return _typeId; }
    }
}
Run Code Online (Sandbox Code Playgroud)

它适用的数据模型:

public class RegistrationData
{
    [Required(ErrorMessage = "First Name Required")] …
Run Code Online (Sandbox Code Playgroud)

c# validation asp.net-4.0 data-annotations asp.net-mvc-2

7
推荐指数
1
解决办法
4266
查看次数