title: How do I access the ModelState from an ActionFilter?
body: <p>I'm building an <code>ActionFilter</code> to reuse some code for a simple spam block - basically what I do is that I have a <code>Html Helper</code> method that renders an input textbox and a hidden input, and in the ActionFilter I check whether the two values are the same or not. If not, I want to leverage the rest of my validation logic and add a <code>ModelStateError</code> to the <code>ModelState</code>, but how do I do that? How do I add a <code>ModelStateError</code> from whithin the <code>ActionFilter</code>?</p>
  
  <p>UPDATE: Here's the code I'm trying with. When I test a controller action that has this attribute, <code>ModelState.IsValid</code> still returns <code>true</code> even though I don't pass any of the form values required:</p>
  
  <pre><code>public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
      var r = filterContext.HttpContext.Request;
      if (r.Form["sixtimesnine"] != r.Form["fourtytwo"] || string.IsNullOrEmpty(r.Form["sixtimesnine"]) || string.IsNullOrEmpty(r.Form["fourtytwo"]))
      {
          filterContext.Controller.ViewData.ModelState.AddModelError("Spam", this.ErrorMessage);
      }
      base.OnActionExecuting(filterContext);
  }
  </code></pre>
  
  <p>This is the <code>ActionMethod</code>:</p>
  
  <pre><code>[ValidateAntiSpam(ErrorMessage = "Spambotar får inte.")]
  public ActionResult Write(GuestbookPost postToCreate)
  {
      if (ModelState.IsValid)
      {
          _posts.Add(postToCreate);
          return RedirectToAction("Index");
      }
      return View();
  }
  </code></pre>
  
  <p>I just noticed that if I set a breakpoint inside the <code>OnActionExecuting</code> method and hit "Debug tests", the breakpoint is never hit. Why?</p>