c# - StringLengthAttribute 和本地化文本

标签 c# .net data-annotations

以下代码是从 MSDN 上抓取的:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx

[MetadataType(typeof(ProductMetadata))]
public partial class Product
{

}

public class ProductMetadata
{

    [ScaffoldColumn(true)]
    [StringLength(4, ErrorMessage = "The ThumbnailPhotoFileName value cannot exceed 4 characters. ")]
    public object ThumbnailPhotoFileName;

}

如何将本地化文本(例如:来自资源文件)应用于错误消息?

最佳答案

使用 ValidationAttribute.ErrorMessageResourceType属性引用您的资源文件,ValidationAttribute.ErrorMessageResourceName属性引用该资源文件中字符串的名称。例如:

[StringLength(4, ErrorMessageResourceType = typeof(YourResourceFileHere), ErrorMessageResourceName = "NameOfStringInResourceFile")]

您还可以查看 this blog post如果您需要更多示例。

关于c# - StringLengthAttribute 和本地化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3398960/

相关文章:

c# - POST Controller 操作方法具有 NULL ViewModel 参数

c# - Visual Studio 的属性面板中带有子属性的自定义属性

c# - MaxLength + Asp.net Web API - 错误

c# - 如何在 Blazor Server 中实现自定义授权

c# - C# 中带有列表的默认字典

.net - IIS 中的单例类是线程安全的吗?

c# - 使用数据注释分配 DateTime 的格式?

c# - 根据国家选择更改货币格式

c# - NAudio 演示不再有效

c# - 使用 ComponentModel.DataAnnotations 对英国日期格式进行 MVC3 验证(也使用 jquery ui datepicker)