我正在本地化我的asp.net Mvc应用。字符串放在资源文件中。我正在用System.ComponentModel.DataAnnotations.DisplayAttribute属性装饰我的模型,例如:
public class User
{
public virtual Guid Id { get; set; }
[Display(ResourceType = typeof(ModelRes.User), Name = "LogonName")]
public virtual string LogonName { get; set; }
[Display(ResourceType = typeof(ModelRes.User), Name = "Password")]
public virtual string Password { get; set; }
}每个模型(类)都有一个资源文件,User.resx如下所示:
LogonName | "Logon name"
Password | "Password"现在想像一下,对于许多属性,您得到了想法。我想要做的是:
[CustomDisplay(typeof(ModelRes.User))]
public class User
{
public virtual Guid Id { get; set; }
public virtual string LogonName { get; set; }
public virtual string Password { get; set; }
}并且仍然可以在我的(剃刀)视图中使用:
@Html.LabelFor(m => m.LogonName)有实现这样的东西的经验吗?
发布于 2011-07-09 20:52:02
您必须编写一个自定义的ModelMetadataProvider。
然后,您必须使用您的自定义属性和属性名称设置ModelMetadata变量的DisplayName属性。
以下是一些示例/教程
ASP.NET MVC 3 Service Location, Part 7: Model Metadata
Customizing ASP.NET MVC 2 Metadata and Validation
https://stackoverflow.com/questions/6634653
复制相似问题