Reputation: 4369
I'd like to access in my ActionFilter a property. This propert is a collection.
Usually I access the values using the valueprovider like this
filterContext.Controller.ValueProvider.GetValue("Prop");
but this isn't working in case of a collection.
Is there way to get my collection ?
Upvotes: 2
Views: 2366
Reputation: 1039548
You could use filterContext.ActionParameters
. Example:
Model:
public class MyViewModel
{
public IEnumerable<string> Collection { get; set; }
}
Action filter:
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var value = filterContext.ActionParameters["model"];
// TODO: do something with the value
base.OnActionExecuting(filterContext);
}
}
Controller:
public class HomeController : Controller
{
[MyActionFilter]
public ActionResult Index(MyViewModel model)
{
return View();
}
}
Request: /?collection[0]=foo&collection[1]=bar
Upvotes: 4