I am trying to access the Model data passed to the view in the action filter OnActionExecuted. Does anyone know if this is possible?
I am trying to do something like this:
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//get model data
//...
sitemap.SetCurrentNode(model.Name);
}
Any advice?
In .Net Core you have an ActionArguments IDictionary on the context, with all the parameters from your method
So if you have the following controller method
You can access the field like so
If you are getting null - as alternative to @Gustavo Clemente's answer you can try overriding
OnActionExecuted
and passing your viewModel into view in following way:Action:
Attribute:
The model is at:
I don't know why but
filterContext.Controller.ViewData.Model
is always null even when the model bind is executed beforeOnActionExecuted
. I found a solution using theOnModelUpdated
event to set that property before.I have the model binder:
After that you need to set the default binder to your new model binder in
Application_Start()
section in Global.asax:Finally you can access your
Model
in anActionFilterAttribute
: