Question

In a view, I am adding a custom input to the form. This input is not a part of the model that the view is bound to. It is highly customized and depends on the conditions at runtime.

When the controller handles the POST, I can access the posted form field with no problem.

[HttpPost]
[MyCustomActionFilter()]
public ActionResult Edit(int id, IJobType jobType, FormCollection formCollection)
{
    // blah

    string customValue = formCollection["CustomField"]; 

    return View(jobType);
}

I have an ActionFilter that runs after the controller action to handle the custom input, but I see no easy way to access the FormCollection from this filter.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MyCustomActionFilter : FilterAttribute, IActionFilter
 {
    public MyCustomActionFilter()
    {

    }

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // How to access the FormCollection here?
    }
}

Any ideas on how I can easily reference the FormCollection from this filter?

This is for ASP.NET MVC 5.

Thank you!

Was it helpful?

Solution

It seems you could do something like this

var formCollection = new FormCollection(
   filterContext.HttpContext.Request.Form
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top