I am trying to add a WarningCheck attribute to my model, where I’d override the OnActionExecuting to control the validation. The problem is the code is never called.
WarningCheckAttribute
[AttributeUsage(AttributeTargets.All)] // I have tried other targets too without success
public class WarningCheckAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
/* DO SOME STUFF */
}
}
Model
public class Ticket
{
...
[StringLength(50)]
[Display(Name = "Cliente")]
[Required(ErrorMessage = "Il Cliente è obbligatorio.")]
[WarningCheck]
[MaxLength(50, ErrorMessage = "Il nome del Cliente può essere al massimo di 20 cifre."), MinLength(3, ErrorMessage = "Il nome del Cliente è troppo corto. Inserire almeno 3 caratteri.")]
public string Cliente { get; set; }
...
As the name says, Action FilterAttribute should be applied to Actions, not properties.