【问题标题】:Extension Method for DevExpress GridView ColumnsDevExpress GridView 列的扩展方法
【发布时间】:2013-11-27 19:36:52
【问题描述】:

如何扩展 DevExpress GridView 列。 DevExpress 似乎无法跟随 DataAnnotations Display。

这是我为实现此行为而汇总的内容:

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

我怎样才能实现这种行为?

【问题讨论】:

    标签: c# asp.net-mvc devexpress extension-methods


    【解决方案1】:
    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);
            });
        }
    }
    

    或者不那么冗长

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

    然后在视图中

    settings.Columns.AddModelToGrid<YourType>();
    

    【讨论】:

    • 太好了,我仍然不明白为什么其他方式不起作用。该错误没有说明模型是问题所在。
    • 这里你只是告诉 GridView 它必须显示哪些属性(你将它绑定到一个类型),而不是当你调用 @grid.Bind(Model).GetHtml() 时你传递实际的模型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多