将List <enum>参数传递到ASP.NET MVC3中的自定义操作过滤器</enum>

时间:2011-07-13 08:51:43

标签: asp.net-mvc asp.net-mvc-3

我如何将List解析为自定义动作过滤器(如输入参数)?

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()
{
}

我得错了错误

'InputParameter' is not a valid named attribute argument because it is not a valid attribute parameter type

1 个答案:

答案 0 :(得分:12)

操作过滤器参数是操作过滤器的属性:

[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);
  }
}

属性参数类型仅限于此处描述的类型 - http://msdn.microsoft.com/en-us/library/aa664615%28v=vs.71%29.aspx

您可以通过属性构造函数传递集合,如此处所述 - Can I initialize a C# attribute with an array or other variable number of arguments?