【问题标题】:Extend Grid.Mvc Annotations with ressource file as header source使用资源文件作为标头源扩展 Grid.Mvc 注释
【发布时间】:2017-02-02 02:58:18
【问题描述】:

我正在使用 Grid.Mvc 框架来展示我的模型数据。

请看:Source CodeDocumentation

开箱即用有两个选项来显示列标题

第一:

没有资源文件..

//Annotation
[GridColumn(Title = "Active Foo?")]
public bool Enabled { get; set; }


[GridColumn(Title = "Date", Format = "{0:dd/MM/yyyy}")]
public DateTime FooDate { get; set; }

...

//Display the Model items with assigned Column Titles
@Html.Grid(Model).AutoGenerateColumns()

第二:

在视图中使用资源字符串..

//Assign Column Header from 
@Html.Grid(Model).Columns(columns =>
{
        columns.Add(n => n.Enabled).Titled(DisplayFieldNames.Enabled); 
        columns.Add(n => n.FooDate).Titled(DisplayFieldNames.FooDate);
})

我想知道如何扩展第一种方法(在模型中使用数据注释)

类似:

[GridColumn(Title ="Enabled", ResourceType = typeof(DisplayFieldNames))]

[GridColumn(Title = "Date", ResourceType = typeof(DisplayFieldNames), Format = "{0:dd/MM/yyyy}")]

内部的 ResourceType 属性应该使网格在我的资源文件“DisplayFieldNames”中查找列标题

【问题讨论】:

    标签: c# asp.net-mvc annotations grid.mvc


    【解决方案1】:

    在朋友的支持下,我找到了自己的解决方案。

    就是这样,希望对其他人也有帮助。

    public class LocalizedGridCoulmnAttribute : GridColumnAttribute
    {
    
        private Type _resourceType;
    
        /// <summary>
        /// The type of the Ressource file 
        /// </summary>
        public Type ResourceType
        {
            get
            {
                return this._resourceType;
            }
            set
            {
                if (this._resourceType != value)
                {
                    ResourceManager rm = new ResourceManager(value);
                    string someString = rm.GetString(LocalizedTitle);
    
                    this._resourceType = value ?? value;
                }
                if (ResourceType != null && LocalizedTitle != String.Empty)
                {
                    ResourceManager rm = new ResourceManager(ResourceType);
                    Title = rm.GetString(LocalizedTitle);
                }
            }
        }
    
        /// <summary>
        /// Overrides The Title Property of GridColumnAttribute
        /// Works with Ressourcefile
        /// </summary>
        public string LocalizedTitle
        {
            set {
                if (ResourceType != null && value != String.Empty)
                {
                    ResourceManager rm = new ResourceManager(ResourceType);
                    Title = rm.GetString(value);
                }
                else Title = value ?? value;
            }
            get { return Title; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      相关资源
      最近更新 更多