Asp.net mvc 在asp mvc中为模型使用特定于语言的资源
我对如何使用预定义模型在视图中显示基于语言的通知感兴趣 例如,这是我的模型的一部分:Asp.net mvc 在asp mvc中为模型使用特定于语言的资源,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我对如何使用预定义模型在视图中显示基于语言的通知感兴趣 例如,这是我的模型的一部分: public class PrivateModel { [Required(ErrorMessage = "Please fill in your last name.")] [Display(Name = "First Name")] public string FirstName { get; set; } [Required(Err
public class PrivateModel
{
[Required(ErrorMessage = "Please fill in your last name.")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please fill in your last name.")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
}
以下是观点:
@using(Html.BeginForm())
{
<div>
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m=>m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName)
</div>
<div>
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m=>m.LastName)
@Html.ValidationMessageFor(m => m.LastName)
</div>
<div>
<input type="submit" value="First click"
</div>
而不是
@Html.ValidationMessageFor(m => m.FirstName)
使用
您可以使用:
ErrorMessageResourceName
数据注释属性的
然后,您可以将所有消息存储在本地化的资源文件中,然后该键将根据选定的本地化查找字符串
全球化是一个庞大的话题,但这应该让您开始。请参阅以下链接以获取本地化完整指南
你能使用资源文件吗?@DanielJ.G。我已经有一个数据库,我必须使用我必须使用数据库,而不是资源文件。ErrorMessageResourceName会解决显示问题吗?基本上,如果唯一的解决方案是使用资源文件,我只想知道如何实现我提供的想法。我在这里质疑体系结构。在我看来,混合演示和数据库并不是一个经过深思熟虑的过程。除非数据库是专门为此设计的,并且是与应用程序数据库分开的实体。每个页面视图将针对数据库生成一个查询堆栈,用于查询每个所需字符串。你真的想让这种流量对每个页面视图都影响数据库吗?数据库结果正在被缓存,但无法使用资源文件,因此你必须声明自己的并使用LocalizedDisplayNameAttribute。
@Html.ValidationMessageFor(m => GetSource(m.FirstName, culture))
ErrorMessageResourceName
public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
private PropertyInfo _nameProperty;
private Type _resourceType; //Declare Cache object here to select property name
public LocalizedDisplayNameAttribute(string displayNameKey)
: base(displayNameKey)
{
}
public Type NameResourceType
{
get
{
return _resourceType;
}
set
{
_resourceType = value;
//initialize nameProperty when type property is provided by setter
// _nameProperty = _resourceType.GetProperty(base.DisplayName, BindingFlags.Static | BindingFlags.Public);
_nameProperty = select property from Cache values for Database
}
}
public override string DisplayName
{
get
{
//check if nameProperty is null and return original display name value
if (_nameProperty == null)
{
return base.DisplayName;
}
return (string)_nameProperty.GetValue(_nameProperty.DeclaringType, null);
}
}