asp.net-mvc-3 – ASP.NET MVC 3: – 使用数据库而不是资源文件作为本地化存储
发布时间:2020-05-28 13:36:50 所属栏目:asp.Net 来源:互联网
导读:我们在数据库中有本地化的字符串,并想知道 extending the ASP.NET Resource Provider Model是否可以与ASP.NET MVC 3 Razor视图引擎一起使用. 请告诉我,一旦我们扩展了ASP.NET资源提供程序模型,ASP.NET MVC 3 Razor视图引擎是否支持从数据库中检索本地化字符串
|
我们在数据库中有本地化的字符串,并想知道 extending the ASP.NET Resource Provider Model是否可以与ASP.NET MVC 3 Razor视图引擎一起使用. 请告诉我,一旦我们扩展了ASP.NET资源提供程序模型,ASP.NET MVC 3 Razor视图引擎是否支持从数据库中检索本地化字符串.或者它只适用于Classic ASP.NET而不适用于ASP.NET MVC. 谢谢 Satyaprakash J. 解决方法到目前为止我发现的最干净的解决方案是: http://www.codeproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC.欢迎提出意见/反馈. 编辑1:根据评论,我添加了代码示例并使用该链接作为参考. 我创建了一个customDataAnnotationsProvider类: public class CustomDataAnnotationsProvider: DataAnnotationsModelMetadataProvider
{
private ResourceManager resourceManager = new ResourceManager();
protected override ModelMetadata CreateMetadata(
IEnumerable<Attribute> attributes,Type containerType,Func<object> modelAccessor,Type modelType,string propertyName)
{
string key = string.Empty;
string localizedValue = string.Empty;
foreach (var attr in attributes)
{
if (attr != null)
{
if (attr is DisplayAttribute)
{
key = ((DisplayAttribute)attr).Name;
if (!string.IsNullOrEmpty(key))
{
localizedValue = resourceManager.GetLocalizedText(key);
((DisplayAttribute)attr).Name = localizedValue;
}
}
else if (attr is ValidationAttribute)
{
key = ((ValidationAttribute)attr).ErrorMessage;
if (!string.IsNullOrEmpty(key))
{
localizedValue = resourceManager.GetLocalizedText(key);
((ValidationAttribute)attr).ErrorMessage = localizedValue;
}
}
}
}
return base.CreateMetadata(attributes,containerType,modelAccessor,modelType,propertyName);
}
}
然后我在Global.asax中的ApplicationStart上引用了自定义提供程序 ModelMetadataProviders.Current = new Project.Web.Helpers.CustomDataAnnotationsProvider(); 您不必更改模型,可以使用显示注释: [Display(Name = "CustomerAccountNumber")]
public string CustomerAccountNumber { get; set; } (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.Net MVC中的线程安全性
- asp.net – 如何将服务器时间转换为本地时间
- 我在哪里可以报告ASP.NET浏览器文件的问题?
- asp.net – Xdocument不打印声明
- asp.net – 如何从SQL Server 2008本身获取客户端IP地址?
- asp.net – 在JavaScript中访问asp:hiddenfield控件
- asp.net-mvc – ASP.NET MVC表单身份验证和未经身份验证的控
- asp.net-mvc – 适当地url编码空间字符
- asp.net – html文本框的最大长度是多少?
- asp.net-mvc-3 – 通过URL导致控制器的DateTime导致ASP .NE
