Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the ModelState from an ActionFilter?

I'm building an ActionFilter to reuse some code for a simple spam block - basically what I do is that I have a Html Helper 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 ModelStateError to the ModelState, but how do I do that? How do I add a ModelStateError from whithin the ActionFilter?

UPDATE: Here's the code I'm trying with. When I test a controller action that has this attribute, ModelState.IsValid still returns true even though I don't pass any of the form values required:

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

This is the ActionMethod:

[ValidateAntiSpam(ErrorMessage = "Spambotar får inte.")] public ActionResult Write(GuestbookPost postToCreate) {     if (ModelState.IsValid)     {         _posts.Add(postToCreate);         return RedirectToAction("Index");     }     return View(); } 

I just noticed that if I set a breakpoint inside the OnActionExecuting method and hit "Debug tests", the breakpoint is never hit. Why?

like image 493
Tomas Aschan Avatar asked Jun 01 '09 07:06

Tomas Aschan


People also ask

How do I access ModelState?

The ModelState can be accessed in View using the ViewData object in ASP.Net MVC Razor.

What is ModelState IsValid method where we use it?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

How do I find ModelState errors in view?

To pass error to the view we can use ModelState. AddModelError method (if the error is Model field specific) or simply ViewBag or ViewData can also be used.


1 Answers

That would be by: filterContext.Controller.ViewData.ModelState

like image 77
Gideon Avatar answered Nov 13 '22 22:11

Gideon