MVC 3: ActionFilterAttribute и OnActionExecuting не запускаются - PullRequest
2 голосов
/ 04 марта 2012

Я пытаюсь добавить атрибут WarningCheck в мою модель, где я бы переопределил OnActionExecuting для управления проверкой.Проблема в том, что код никогда не вызывается.

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 */
    }
}

Модель

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

1 Ответ

3 голосов
/ 04 марта 2012

Как следует из названия, Действие FilterAttribute должен применяться к Действиям , а не к свойствам.

[WarningCheck]
public ActionResult Create(Ticket ticket)
{

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...