Search code examples
asp.net-mvcasp.net-mvc-3model-bindingmodelbindersactionfilterattribute

ASP.NET MVC 3: Can I pass a modelbound object to a FilterAttribute?


I have a controller method with a custom FilterAttribute on it...

[ActivityHistory]
public ActionResult Index(Vehicle vehicle, string componentName)
{
    return PartialView("_Description");
}

The Activityhistory class...

public class ActivityHistoryAttribute : FilterAttribute, IResultFilter
{
    public void OnResultExecuting(ResultExecutingContext filterContext)
    {
      //I would like to use the Vehicle object passed into the controller here
      //The cotroller call is made from jQuery and json, the ASP.NET MVC modelbinding
      // is creating the c# object instance.  Can I resuse this object within this method? 
    }
 }

That's pretty much my question, can I reuse the modelbound Vehicle object in my custom attribute class? How do I pass a reference to that object to the custom attribute class.

Thanks for any tips or ideas on how I can accomplish this, if its even possible.

Cheers, ~ck in San Diego


Solution

  • var vehicle = filterContext.ActionParameters["vehicle"] as Vehicle