更改默认值“{0} 字段为必填项” (最终解决方案?)
再会!
我有以下用于登录表单的 ViewModel 类:
using System.ComponentModel.DataAnnotations;
...
public class UserLogin : IDataErrorInfo
{
[Required]
[DisplayName("Login")]
public string Login { get; set; }
[Required]
[DisplayName("Password")]
public string Password { get; set; }
[DisplayName("Remember Me")]
public bool RememberMe { get; set; }
#region IDataErrorInfo Members
// This will be a Model-level error
public string Error
{
get
{
if (!WebUser.CanLogin(Login, Password))
{
return Resources.ValidationErrors.InvalidLoginPassword;
}
else
{
return String.Empty;
}
}
}
// All is handled by DataAnnotation attributes, just a stub for interface
public string this[string columnName]
{
get
{
return string.Empty;
}
}
#endregion
}
这在 Global.asax
中:
DefaultModelBinder.ResourceClassKey = "BinderMessages";
ValidationExtensions.ResourceClassKey = "BinderMessages";
资源文件 BinderMessages.resx
放置在 App_GlobalResources 内,它有两个键 InvalidPropertyValue
(有效)和 PropertyValueRequired
无效,并给我默认消息。
问题:是否可以修改此消息,或者它与 DataAnnotations 相关?
我发现了很多关于此的帖子,但没有解决方案。现在我只是回退到这个:
[Required(ErrorMessageResourceType = typeof(Resources.ValidationErrors), ErrorMessageResourceName = "Required")]
Good day!
I've the following ViewModel class I use for login form:
using System.ComponentModel.DataAnnotations;
...
public class UserLogin : IDataErrorInfo
{
[Required]
[DisplayName("Login")]
public string Login { get; set; }
[Required]
[DisplayName("Password")]
public string Password { get; set; }
[DisplayName("Remember Me")]
public bool RememberMe { get; set; }
#region IDataErrorInfo Members
// This will be a Model-level error
public string Error
{
get
{
if (!WebUser.CanLogin(Login, Password))
{
return Resources.ValidationErrors.InvalidLoginPassword;
}
else
{
return String.Empty;
}
}
}
// All is handled by DataAnnotation attributes, just a stub for interface
public string this[string columnName]
{
get
{
return string.Empty;
}
}
#endregion
}
And this in Global.asax
:
DefaultModelBinder.ResourceClassKey = "BinderMessages";
ValidationExtensions.ResourceClassKey = "BinderMessages";
The resource file BinderMessages.resx
is placed inside App_GlobalResources it has two keys InvalidPropertyValue
(which works) and PropertyValueRequired
which doesn't and gives me default message.
Question: Is it possible to modify this message, or it's tied to DataAnnotations?
I've found many posts about this, but without solution. For now I just fallback to this:
[Required(ErrorMessageResourceType = typeof(Resources.ValidationErrors), ErrorMessageResourceName = "Required")]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个扩展
RequiredAttribute
的自定义ValidationAttribute
并在其中设置值。类似于:然后用您的自定义属性装饰您的模型。
默认消息编译到
System.ComponentModel.DataAnnotations.Resources.DataAnnotationsResources.resources
下资源文件中的 DataAnnotations 程序集中,并且RequiredAttribute_ValidationError=The {0} 字段是必需的。
。所以回答你的问题,是的,该消息是 DataAnnotations 的一部分。编辑:PropertyValueRequired 用于处理不可为空类型的空值错误。如下所述,
PropertyValueInvalid
用于类型转换错误。You can create a custom
ValidationAttribute
that extendsRequiredAttribute
and sets the values there. Something like:Then decorate your Model with your custom attribute.
The default message is compiled into the DataAnnotations assembly in the resource file under
System.ComponentModel.DataAnnotations.Resources.DataAnnotationsResources.resources
and isRequiredAttribute_ValidationError=The {0} field is required.
. So to answer your question, yes, that message is part of DataAnnotations.Edit:
PropertyValueRequired
is used for errors on null values with non-nullable types. As mentioned belowPropertyValueInvalid
is used for type conversion errors.我已经完成了一种使用单例类来提供翻译的方法。您仍然需要按照 @bmancini 的建议派生所有属性。我的方法的优点是您可以使用多个字符串表(或切换翻译源),而无需修改任何其他逻辑。
由于我的博客文章比较大,我只提供一个链接:
http://blog.gauffin.org/2010/11/simplified-数据注释本地化/
I've done an approach using a singleton class to provide the translations. You still need to derive all attributes as suggested by @bmancini. The upside with my approach is that you can use multiple string tables (or switch translation source) without having to modify any other logic.
Since my blog entry is rather large, I'll just provide a link:
http://blog.gauffin.org/2010/11/simplified-localization-for-dataannotations/