Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing List<Enum> parameters into custom action filter in ASP.NET MVC3

how I can parse List into me custom action filter (like input parameters) ?

public class CustomFilter : ActionFilterAttribute
{

    public List<MyEnumType> InputParameter { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {         
        base.OnActionExecuting(filterContext);
    }
}

[CustomFilter(InputParameter = new List<MyEnumType>() { MyEnumType.Type } )]
public SomeActionInController()
{
}

I got errror Error

'InputParameter' is not a valid named attribute argument because it is not a valid attribute parameter type
like image 295
David Horák Avatar asked Jul 13 '11 08:07

David Horák


1 Answers

Action filter parameters are properties of the action filter:

[CustomFilter(InputParameter=10)]
public SomeActionInController()
{
}

public class CustomFilter : ActionFilterAttribute
{
  public int InputParameter { get; set; }

  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    // access this.InputParameter

    base.OnActionExecuting(filterContext);
  }
}

Attribute parameter types are limited to the types described here - http://msdn.microsoft.com/en-us/library/aa664615%28v=vs.71%29.aspx

You can pass a collection via the attributes constructor as described here - Can I initialize a C# attribute with an array or other variable number of arguments?

like image 81
devdigital Avatar answered Nov 15 '22 11:11

devdigital