ASP.NET MVC Pass object from Custom Action Filter to Action

asp.net MvcFilterCustom Action

asp.net Mvc Problem Overview


If I create an object in a Custom Action Filter in ASP.NET MVC in

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    DetachedCriteria criteria = DetachedCriteria.For<Person>();
    criteria.Add("stuff");

    // Now I need to access 'criteria' from the Action.....

}

is there any way I can access the object from the Action that is currently executing.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

The better approach is described by Phil Haack.

Basically this is what you do:

public class AddActionParameterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        // Create integer parameter.
        filterContext.ActionParameters["number"] = 123;

        // Create object parameter.
        filterContext.ActionParameters["person"] = new Person("John", "Smith");
    }
}

The only gotcha is that if you are creating object parameters, then your class (in this case Person) must have a default constructor, otherwise you will get an exception.

Here's how you'd use the above filter:

[AddActionParameter]
public ActionResult Index(int number, Person person)
{
    // Now you can use number and person variables.
    return View();
}

Solution 2 - asp.net Mvc

I would recommend putting it in the Route data.

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.RouteData.Values.Add("test", "TESTING");
        base.OnActionExecuting(filterContext);
    }

    public ActionResult Index()
    {
        ViewData["Message"] = RouteData.Values["test"];

        return View();
    }

Solution 3 - asp.net Mvc

You could use the HttpContext:

filterContext.HttpContext.Items["criteria"] = criteria;

And you can read it in the action:

[YourActionFilter]
public ActionResult SomeAction() 
{
    var criteria = HttpContext.Items["criteria"] as DetachedCriteria;
}

Solution 4 - asp.net Mvc

Set item in ViewData or of a viewmodel if you pass it as a parameter into your action. Here I set the property of a ViewModel

public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     ViewModelBase viewModel = null;
     foreach (object parameter in filterContext.ActionParameters.Values)
     {
         if (parameter is ViewModelBase)
         {
             viewModel = (ViewModelBase)parameter;
             break;
         }
     }
     if(viewModel !=null)
     {
         viewModel.SomeProperty = "SomeValue";
     }
 }


    public ActionResult About(ViewModelBase model)
    {
      string someProperty= model.SomeProperty;
}

Here is the untyped version I think you prefer:

   public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData.Add("TestValue", "test");
        
    }

       [FilterWhichSetsValue]
        public ActionResult About()
        {
            string test = (string)ViewData["TestValue"];
            return View();
        }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionreach4thelasersView Question on Stackoverflow
Solution 1 - asp.net MvcniaherView Answer on Stackoverflow
Solution 2 - asp.net MvcChad MoranView Answer on Stackoverflow
Solution 3 - asp.net MvcDarin DimitrovView Answer on Stackoverflow
Solution 4 - asp.net MvcMathias FView Answer on Stackoverflow