2017-02-01 112 views
1

我正在使用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() 

二:

使用的ressource字符串中查看..

//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))] 

and

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

的ResourceType屬性裏面應該使電網內部查找我的ressource文件「DisplayFieldNames」

回答

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