1

hi im trying to create the next simple custom validation:

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"
        };
    }
}

and the use it like:

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

but im keep geting the next error:

Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

EricGS
  • 1,323
  • 2
  • 17
  • 42
  • 4
    People, if you are going to down vote, it's courtesy to at least give your reason why. – Paul Oct 08 '13 at 13:01
  • It works for me, are you sure you are trying to use it like `[EnteroDistintoDe(0)]`? That error occurs when you try to use non-constants like a function call. Please post the exact property you are having trouble with – Jun Wei Lee Oct 08 '13 at 13:34
  • is that exact code, the aplication wont compile becouse of this error – EricGS Oct 08 '13 at 13:49
  • sometimes one just have to take a brake, shut down the pc, and go lunch, then you restart your pc and all works whitnout any change – EricGS Oct 08 '13 at 14:18
  • You have not specified an error message, either a constant string or a pair of resourcename/resourcetype. Those are necessary parameters. Beyond that you can add `Items`. – Tevya Apr 06 '15 at 15:59

0 Answers0