2012-11-14 4 views
2

다이내믹 ExpandoObjects 컬렉션이 포함 된 Telerik MVC를 사용하려고합니다.ExpanderObject 목록이있는 Telerik MVC 그리드

컨트롤러는 다음과 같습니다

[GridAction] 
public ActionResult TestDiario() 
{ 
     var result = new List<dynamic>(); 

     dynamic diarioModel = new ExpandoObject(); 

     var dictionary = (IDictionary<string, object>)diarioModel; 

     dictionary.Add("ID", 1); 
     dictionary.Add("AlunoID", 12781); 
     dictionary.Add("DAY05CLASS1", true); 
     dictionary.Add("DAY05CLASS2", true); 
     dictionary.Add("DAY07CLASS1", true); 
     dictionary.Add("DAY08CLASS1", true); 

     result.Add(diarioModel); 
     return View(result); 
} 

보기는 다음과 같습니다

@using Telerik.Web.Mvc.UI 

@model IEnumerable<dynamic> 

@{ 
    ViewBag.Title = "TestDiario"; 
} 

@(Html.Telerik().Grid(Model).Name("Grid") 
    .DataKeys(dataKeys => dataKeys.Add("ID")) 
    .Columns(columns => 
    { 
     columns.Bound("MatAnoID").Visible(true); 
     columns.Bound("AlunoID"); 
     columns.Bound("NroClasse"); 
     columns.Bound("Aluno"); 

     var dictionary = (IDictionary<string, object>)Model; 
     foreach (var property in (IDictionary<String, Object>)dictionary) 
     { 
      if (property.Key.ToString().Remove(3) == "DAY") 
      { 
       columns.Bound(property.Key); 
      } 
     } 
    }) 
    .Pageable() 
    .Sortable() 
    .Groupable() 
    .Filterable() 

) 

루프 foreach 문이 DAY 문자열에 의해 시작 역학 필드를 가져옵니다. 내가 프로젝트를 실행하면

은 다음과 오류가 나타납니다

{ "는 입력 객체 System.Collections.Generic.List 1[System.Object]' to type 'System.Collections.Generic.IDictionary 2 [선택 System.String,하는 System.Object]을 '변환 할 수 없습니다."}

필드를 통한 루프가있는 Telerik MVC 컨트롤과 함께 동적 객체를 사용하는 방법이 있습니까?

답변

2

예, 할 수 있습니다. 아주 가깝지만 몇 가지 실수를 저 지르 셨습니다.

1) 확장 개체가 아니므로 모델 IDictionary 유형이 아닙니다. 동적 목록입니다.

이것은 해킹 비트이지만 문제를 해결하기 위해 열거 형의 첫 번째 (또는 기본) 요소를 가져 와서 사전을 작성했습니다.

2) 확장 개체에 속성으로 존재하지 않는 열을 바인딩하려고합니다.

나는 그것들을 주석 처리했다.

3) I 생각해 보면 "DAY"로 시작하는 키를 찾고있었습니다. 열 이름에서 "DAY"를 제거하려면 예제 코드를 조정할 수 있습니다. 잘 작동 이외의

:

@(Html.Telerik().Grid(Model).Name("Grid") 
    .DataKeys(dataKeys => dataKeys.Add("ID")) 
    .Columns(columns => 
    { 
     //NOTE: some of these columns are not valid because you didn't include them as properties 
     //columns.Bound("MatAnoID").Visible(true); 
     //columns.Bound("NroClasse"); 
     //columns.Bound("Aluno"); 

     columns.Bound("AlunoID"); 

     var first = Model.FirstOrDefault(); 
     if (first != null) { 
      var dictionary = (IDictionary<string, object>)first; 
      foreach (var property in dictionary) { 
       string key = property.Key.ToString(); 
       if (key.StartsWith("day", StringComparison.InvariantCultureIgnoreCase)) { 
        columns.Bound(property.Key); 
       } 
      } 
     } 

    }) 
    .Pageable() 
    .Sortable() 
    .Groupable() 
    .Filterable() 
) 
+0

감사합니다, 그것은 작품! – user1822331

관련 문제