gpt4 book ai didi

asp.net-mvc - 本地化数据注释默认消息([必需] [StringLength]等)

转载 作者:行者123 更新时间:2023-12-03 11:47:40 24 4
gpt4 key购买 nike

如果我使用如下属性装饰ViewModels的属性:

public class Vm
{

[Required]
[StringLength(35)]
public string Name {get;set;}

}

我将收到英语验证消息:
"this field is required"
"The field Name must be a string with a maximum length of 35"

我该如何翻译?

最佳答案

您可以使用 ErrorMessageResourceName 属性:

[Required(ErrorMessageResourceName = "SomeResource")]
[StringLength(30, ErrorMessageResourceName = "SomeOtherResource")]
public string Name { get; set; }

您可以查看 this blog post为例。

更新:

Application_Start中:
DefaultModelBinder.ResourceClassKey = "Messages";

并且需要在 Messages.resx文件中添加自定义错误消息。使用Reflector查看 System.Web.MvcSystem.ComponentModel.DataAnnotations程序集,以查看要使用的键名。

关于asp.net-mvc - 本地化数据注释默认消息([必需] [StringLength]等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3758512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com