2014-09-01 2 views
0

내가 사용하고 속성 정의가 비어 있습니다 그런 다음보기에서 사용자 지정 특성 값을 읽고 싶습니다. Type type = Model.GetType();ASP.NET MVC5 사용자 정의 속성은

@model AggregationFiltersViewModel 
@{ 
    Type type = Model.GetType(); 
    ExcludeFilterAttribute[] AttributeArray = (ExcludeFilterAttribute[])type.GetCustomAttributes(typeof(ExcludeFilterAttribute), false); 

    ExcludeFilterAttribute fa = AttributeArray[0]; 
} 

그런

@if (fa.FilterToExclude != "Categories")   
{ 
    <th>Category:</th> 
    <td>@Html.DropDownListFor(m => m.SelectedCategoryId, Model.Categories)</td> 
} 

그러나, 사용자 정의의 배열이 속성은 비어 있습니다, 그래서 나는 다음과 같은 오류 얻을 :

Index was outside the bounds of the array. System.IndexOutOfRangeException: Index was outside the bounds of the array. 

가 어떻게 사용자 정의 속성의 값을 얻을 수 있습니까? 값 모델 변수를 전달할 수 있지만 커스텀 애트리뷰트를 사용하면 커다란 콜렉션을 제외시킬 때 더 쉬워진다.

+1

속성은 모델을 볼 수 없습니다, 컨트롤러 클래스의'AggregationClientBase' 방법 내에서 정의된다. –

답변

1

모델에서 속성을 가져 오려고하지만 컨트롤러의 AggregationClientBase 메 서드에 정의되어 있습니다.

은 그래서 :

var controllerType = typeof(YourController); 
var method = controllerType.GetMethod("AggregationClientBase"); 
var parameter = method.GetParameters().First(p => p.Name == "filters"); 

var fa = parameter.GetCustomAttributes(typeof(ExcludeFilterAttribute), false) 
        .First() as ExcludeFilterAttribute; 
+0

이것은 메서드에 대한 첫 번째 매개 변수를 정의하는 것을 의미하지 않습니까? 'public ActionResult AggregationClientBase ([ExcludeFilter ("Categories")] AggregationFiltersViewModel filters)' –

+0

죄송하지만, 귀하의 문장을 얻을 수 없습니다. 내가 이름을 말한거야? 또는 무엇을? –

+0

미안 해요, 난 '[ExcludeFilter ("카테고리")] 퍼팅 생각했다 AggregationFiltersViewModel는 은 필터에 대한 속성이 모델을 볼 정의 filters'? –