user256034
user256034

Reputation: 4369

asp.net mvc - value provider and collections

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

Answers (1)

Darin Dimitrov
Darin Dimitrov

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

Related Questions