2011-07-13 45 views
6

我如何解析列表到我自定義操作篩選器(如輸入參數)?傳遞列表<Enum>參數到ASP.NET MVC3中的自定義操作篩選器

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

我errror錯誤

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

回答

11

行動濾波器參數是動作過濾器的性能:

[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

您可以通過屬性constru傳遞一個集合這裏所描述的ctor - Can I initialize a C# attribute with an array or other variable number of arguments?

+0

這項工作很好,但如果我解析名單然後我得到了錯誤,請檢查後期編輯 –

+0

我已經更新了我的答案 – devdigital

+0

感謝,對鏈接... –

相關問題