将参数传递给自定义验证属性

时间:2013-10-08 12:41:52

标签: c# asp.net-mvc-4 validationattribute

我想尝试创建下一个简单的自定义验证:

public class EnteroDistintoDeAttribute : ValidationAttribute, IClientValidatable
{
    public int entero { set; get; }

    public EnteroDistintoDeAttribute(int entero)
    {
        this.entero = entero;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {   
            if(entero == (int)value)
            {
                return new ValidationResult(Res_Errors.errorDistinto);
            }                   
        return ValidationResult.Success;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessage,
            ValidationType = "EnteroDistintoDe"
        };
    }
}

并使用它:

[EnteroDistintoDe(0)]
[Required]       
public int scheduledIdPersonaSeleccionada { get; set; }

但我会继续检查下一个错误:

  

错误1属性参数必须是常量表达式typeof   表达式或数组创建表达式的属性参数类型

0 个答案:

没有答案