http://stackoverflow.com/questions/3260748/default-resource-for-data-annotations-in-asp-net-mvc

Add this class somewhere in your project:

 public class ExternalResourceDataAnnotationsValidator : DataAnnotationsModelValidator<ValidationAttribute>
{
/// <summary>
/// The type of the resource which holds the error messqages
/// </summary>
public static Type ResourceType { get; set; } /// <summary>
/// Function to get the ErrorMessageResourceName from the Attribute
/// </summary>
public static Func<ValidationAttribute, string> ResourceNameFunc
{
get { return _resourceNameFunc; }
set { _resourceNameFunc = value; }
}
private static Func<ValidationAttribute, string> _resourceNameFunc = attr => attr.GetType().Name; public ExternalResourceDataAnnotationsValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
: base(metadata, context, attribute)
{
if (Attribute.ErrorMessageResourceType == null)
{
this.Attribute.ErrorMessageResourceType = ResourceType;
} if (Attribute.ErrorMessageResourceName == null)
{
this.Attribute.ErrorMessageResourceName = ResourceNameFunc(this.Attribute);
}
}
}

and in your global.asax, add the following:

// Add once
ExternalResourceDataAnnotationsValidator.ResourceType = typeof(CustomDataAnnotationsResources); // Add one line for every attribute you want their ErrorMessageResourceType replaced.
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RangeAttribute), typeof(ExternalResourceDataAnnotationsValidator));

It will look for a property with the same name as the validator type for the error message. You can change that via the ResourceNameFunc property.

EDIT: AFAIK this works from MVC2 onwards, as DataAnnotationsModelValidatorProvider was introduced in MVC2.


To achieve this, I created a new class that inherits from RequiredAttribute, and the error message is embedded inside this new class:

The error message is taken from the ValidationResource.resx file, where I list the error message as follows:

public class RequiredWithMessageAttribute : RequiredAttribute
{
public RequiredWithMessageAttribute()
{
ErrorMessageResourceType = typeof(ValidationResource);
ErrorMessageResourceName = "RequiredErrorMessage";
}
}

RequiredErrorMessage --> "{0} is required."

where {0} = display name.

I then annotate my models like this, so I never have to repeat my error message declarations:

[RequiredWithMessage]
public string Name { get; set; }

Once you do this, an error message ("Name is required.") will appear whenever validation fails.

This works properly with ASP.NET MVC's server-side validation and client-side validation.

MVC 多语言记录1 设置默认的ResourceType的更多相关文章

  1. ng-option指令使用记录,设置默认值需要注意

    ng-options一般有以下用法: 数组作为数据源: label for value in array select as label for value in array label group ...

  2. cefsharp设置默认语言

    cefsharp是不错的浏览器内核封装版本之一,默认语言是en-US,这个一直困扰着项目,项目好多处需修改,后来经多次尝试,才发现,原来设置默认语言这么简单. CefSharp.Settings se ...

  3. 如何将ASP.NET MVC所有参数均自动设置为默认

    今天看到CSDN上有个问题觉得有点意思:"可不可以ASP.NET MVC所有参数均自动设置为默认" public class HomeController : Controller ...

  4. Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数

    Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数 7.4.4  为外部参数设置默认值 开发者也可以对外部参数设置默认值.这时,调用的时候,也可以省略参数传递本文选自Swift1 ...

  5. 我的Android进阶之旅------>Android 设置默认语言、默认时区

    1. 设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist.sys.timez ...

  6. abp 设置默认语言为中文

    abp 设置默认语言为中文 abp的默认语言设置,存放于数据库表AbpSettings中,这样配置可使默认语言为中文: name: Abp.Localization.DefaultLanguageNa ...

  7. linux / centos 安装SQL Server 2017 设置默认语言与排序规则Chinese_PRC_CI_AS

    安装 安装很简单参照官方教程 https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sq ...

  8. gorm创建记录及设置字段默认值

    package main import ( "database/sql" "gorm.io/driver/mysql" "gorm.io/gorm&q ...

  9. 12天学好C语言——记录我的C语言学习之路(Day 6)

    12天学好C语言--记录我的C语言学习之路 Day 6: 今天,我们要开始学习数组了. //①数组部分,数组的大小不能够动态定义.如下: //int n;   scanf("%d,& ...

随机推荐

  1. Swift 自定义Subscript

    Swift可以方便给自定义类加下标,其中参数和返回值可以在类里定义为任意类型: subscript(parameters) -> ReturnType { get { //return some ...

  2. js动态显示表格的汇总信息和详细信息

    我在做数据结果展示的时候,想要实现一个如下的功能:    用户可以选择一个时间段,默认显示这个时间段的汇总数据,当鼠标点击这个时间段的时候,将显示每个时间点的详细数据,再次点击的时候,详细数据收起,只 ...

  3. Android使用SAX解析XML(6)

    应用截图: (1)开始运行时 (2)选择学院 (3)选择专业 (4)选择班级 (5)选择班级的详细信息 本文参考了<Android平台开发之旅>.

  4. [函數] Firemonkey 各平台 "简体" / "繁体" 判断

    目前的 Delphi 10 Seattle 的 TLang 尚不支持取得简体及繁体的语系字符,在能取得正确的语系字符后(整理中),才能使用下列函数判断: function ChineseLang(co ...

  5. 关于mybatis 的mapper namespace 作用及解析

    因为语言惯性,大部分的namespace 在语言级别*来说是作为一种限定性标识来用,起到唯一或一类的标识.来看看语言(以PHP语言为例)上的namespace的作用实例 一.namespace 在PH ...

  6. 控制器层(Controllers)

    本章译者:@freewind 业务逻辑代码通常位于模型(model)层.客户端(比如浏览器)无法直接调用其中的代码,所以模型对象提供的功能,必须作为资源以URI方式暴露给外部. 客户端使用HTTP协议 ...

  7. AES .net 、JS 相互加密解密

    /// <summary> /// AES加密 /// </summary> public class AES { /// <summary> /// 加密 /// ...

  8. 7个你可能不认识的CSS单位

    众所周知CSS技术我们虽然很熟悉,在使用的过程却很容易被困住,这让我们在新问题出现的时候变得很不利.随着web继续不断地发展,对于新技术新解决方案的要求也会不断增长.因此,作为网页设计师和前端开发人员 ...

  9. go git 安装配置与使用 (windows 7 64bit)

    go语言安装使用第三方库方式一般采用 go get github.com/.../...  命令.例如: go get github.com/astaxie/beego 1.错误情况一: packag ...

  10. 原生JS:Math对象详解

    Math对象 本文参考MDN做的详细整理,方便大家参考MDN Math 也是一个内置对象, 为数学常量和数学函数提供了属性和方法,而不是一个函数对象. 与其它全局对象不同的是, Math 不是一个构造 ...