C# 如何从资源文件本地化WebGrid列标题
通过添加C# 如何从资源文件本地化WebGrid列标题,c#,asp.net-mvc,gridview,localization,C#,Asp.net Mvc,Gridview,Localization,通过添加display属性,我在应用程序中本地化了属性名称的显示,该属性从resx文件中获取字符串值: public class ViewLeadViewModel { [Required] [Display(Name = "Location", ResourceType = typeof(FormLabels))] public string Location { get; set; } } 这在表单中以及在基本网格中查看数据时都能很好地工作 但是,我想使用WebGr
display
属性,我在应用程序中本地化了属性名称的显示,该属性从resx
文件中获取字符串值:
public class ViewLeadViewModel
{
[Required]
[Display(Name = "Location", ResourceType = typeof(FormLabels))]
public string Location { get; set; }
}
这在表单中以及在基本网格中查看数据时都能很好地工作
但是,我想使用WebGrid
来显示数据,但是似乎不支持Display
属性,我们只能使用DisplayName
来显示数据(当前列标题仅使用属性的实际名称)
我尝试添加此属性:
[DisplayName(FormLabels.ResourceManager.GetString("Location"))]
但是我得到了错误
属性参数必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式
如何从资源文件本地化WebGrid
中的列标题
使现代化 以下是
Index.cshtml
中的代码:
@model IEnumerable<AuroraWeb.Models.ViewLeadViewModel>
@{
var grid = new WebGrid(new List<object>());
grid = new WebGrid( Model,
rowsPerPage: 100);
}
@grid.GetHtml(
tableStyle: "table",
alternatingRowStyle: "alternate")
@model IEnumerable
@{
var grid=new WebGrid(new List());
网格=新的WebGrid(模型,
行数:100);
}
@grid.GetHtml(
表样式:“表”,
交替生长样式:“交替”)
您可以创建一个从DisplayNameAttribute
继承的自定义属性类,并通过提供资源键在属性内设置DisplayName
字符串属性,如下例所示:
// provided by Brian Schroer
[AttributeUsage(AttributeTargets.Property)]
public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
public LocalizedDisplayNameAttribute(string resourceKey)
{
ResourceKey = resourceKey;
}
private string ResourceKey { get; set; }
public override string DisplayName
{
get
{
string displayName = FormLabels.ResourceManager.GetString(ResourceKey);
return string.IsNullOrEmpty(displayName) ? string.Format("[[{0}]]", ResourceKey) : displayName;
}
}
}
用法示例:
[LocalizedDisplayName("Location")]
public string Location { get; set; }
参考:
嗨,铁屋,谢谢你的回答。我已经尝试过类似的方法,但它仍然不能用于
WebGrid
。我在DisplayName
类中设置了一个断点,可以看到返回了正确的本地化字符串,但由于某种原因WebGrid
拒绝将该值用于列标题。对不起,我实际上使用的是WebGrid
这是WebGrid
吗?你如何在Razor页面中使用WebGrid
?我用Razor代码更新了我的问题。我相信这就是WebGrid
,是的,我认为columns:grid.columns(grid.Column(“columnName”,FormLabels.ResourceManager.GetString(“key”))
中的方法可以设置本地化的头名称,但我不确定它是否适用于拉入的资源。我在中找到了头名称用法示例。