使用ressource文件作为标头源扩展Grid.Mvc Annotations

时间:2017-02-01 13:42:10

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

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

请注意:Source CodeDocumentation

开箱即用,有两个选项可以显示列标题

首先:

没有ressource文件..

//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”中查找列标题

1 个答案:

答案 0 :(得分:0)

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

在这里,希望它对其他人也有帮助。

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