2013-11-27 4 views
1

어떻게 DevExpress GridView 열을 확장 할 수 있습니까? DevExpress가 DataAnnotations Display를 따라갈 수있는 것으로 보이지 않습니다.DevExpress GridView 열의 확장 메서드

public static class Extensions 
{ 
    public static void AddModelToGrid<T>(
     this MVCxGridViewColumnCollection devExpCollection, 
     List<T> model) 
    { 
     model.ForEach((obj) => 
     { 
      obj.GetType().GetProperties().ToList().ForEach((prop) => 
      { 
       var displayName = 
        (DisplayAttribute)prop.GetCustomAttribute(typeof(DisplayAttribute)); 
       string name = null; 
       if (displayName != null) 
       { 
        name = displayName.Name; 
       } 
       else 
       { 
        name = prop.Name; 
       } 

       devExpCollection.Add(prop.Name, name); 
      }); 
     }); 
    } 
} 

이 아이디어는 다음과 같이 사용하는 것이 었습니다 : 여기

내가이 동작을 달성하기 위해 함께 넣어 것입니다.

@using MyApp.Models 

@{ 
    var grid = Html.DevExpress().GridView(settings => 
    { 
     settings.Name = "GridView"; 
     settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartial" }; 

     settings.KeyFieldName = "ID"; 

     settings.SettingsPager.Visible = true; 
     settings.Settings.ShowGroupPanel = true; 
     settings.Settings.ShowFilterRow = true; 
     settings.SettingsBehavior.AllowSelectByRowClick = true; 

     /* This doesn't work */ 
     settings.Columns.AddModelToGrid<MyObject>(settings.Columns); 

    }); 
    if (ViewData["EditError"] != null) 
    { 
     grid.SetEditErrorText((string)ViewData["EditError"]); 
    } 
} 
@grid.Bind(Model).GetHtml() 

어떻게하면됩니까? 다른 방법이 작동하지 않은 이유

답변

2
public static class Extensions 
{ 
    public static void AddModelToGrid<T>(
     this MVCxGridViewColumnCollection devExpCollection) 
    { 
     // you just need the T parameter, not a list 
     typeof(T).GetProperties().ToList().ForEach((prop) => 
     { 
      DisplayAttribute displayName = null; 

      var attributes = prop.GetCustomAttributes(true); 

      foreach (var attribute in attributes) 
      { 
       if (attribute is DisplayAttribute) 
       { 
        displayName = (DisplayAttribute)attribute; 
       } 
      } 

      string name = null; 
      if (displayName != null) 
      { 
       name = displayName.Name; 
      } 
      else 
      { 
       name = prop.Name; 
      } 

      devExpCollection.Add(prop.Name, name); 
     }); 
    } 
} 

또는

settings.Columns.AddModelToGrid<YourType>(); 
+0

우수한 뷰에서 다음

typeof(T).GetProperties().ToList().ForEach((prop) => { var attributes = prop.GetCustomAttributes(typeof(DisplayAttribute), true); string name = attributes.Length > 0 ? ((DisplayAttribute)attributes[0]).Name : prop.Name; devExpCollection.Add(prop.Name, name); }); 

덜 자세한 정보는, 나는 아직도하지 않습니다. 오류는 모델이 문제가되는 것에 대해 아무런 언급도하지 않았습니다. –

+0

여기서 GridView에'@ grid.Bind (Model) .GetHtml()'을 호출 할 때만 실제 모델을 전달할 때보 다 어떤 속성을 표시해야하는지 (유형에 바인딩하는지)를 말합니다. –

+0

나는 왜 이것이 효과가 있는지, 나는 왜 다른 방법이 없는지 얻지 못한다. –

관련 문제