【问题标题】:MVC 3: ActionFilterAttribute and OnActionExecuting don't fireMVC 3:ActionFilterAttribute 和 OnActionExecuting 不会触发
【发布时间】:2012-03-03 20:07:53
【问题描述】:

我正在尝试将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; }
    ...

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-mvc-3 validation events


    【解决方案1】:

    顾名思义,Action FilterAttribute 应该应用于 Actions,而不是属性。

    [WarningCheck]
    public ActionResult Create(Ticket ticket)
    {
    
    }
    

    【讨论】:

    • 非常感谢!我是新手。
    • @Larry 你应该至少看看那里的一些视频教程。验证的方式比使用操作过滤器更好
    • THX 存档。有什么教程建议吗?
    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 2012-10-11
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多