MVC 多语言记录1 设置默认的ResourceType
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的更多相关文章
- ng-option指令使用记录,设置默认值需要注意
ng-options一般有以下用法: 数组作为数据源: label for value in array select as label for value in array label group ...
- cefsharp设置默认语言
cefsharp是不错的浏览器内核封装版本之一,默认语言是en-US,这个一直困扰着项目,项目好多处需修改,后来经多次尝试,才发现,原来设置默认语言这么简单. CefSharp.Settings se ...
- 如何将ASP.NET MVC所有参数均自动设置为默认
今天看到CSDN上有个问题觉得有点意思:"可不可以ASP.NET MVC所有参数均自动设置为默认" public class HomeController : Controller ...
- Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数
Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数 7.4.4 为外部参数设置默认值 开发者也可以对外部参数设置默认值.这时,调用的时候,也可以省略参数传递本文选自Swift1 ...
- 我的Android进阶之旅------>Android 设置默认语言、默认时区
1. 设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist.sys.timez ...
- abp 设置默认语言为中文
abp 设置默认语言为中文 abp的默认语言设置,存放于数据库表AbpSettings中,这样配置可使默认语言为中文: name: Abp.Localization.DefaultLanguageNa ...
- linux / centos 安装SQL Server 2017 设置默认语言与排序规则Chinese_PRC_CI_AS
安装 安装很简单参照官方教程 https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sq ...
- gorm创建记录及设置字段默认值
package main import ( "database/sql" "gorm.io/driver/mysql" "gorm.io/gorm&q ...
- 12天学好C语言——记录我的C语言学习之路(Day 6)
12天学好C语言--记录我的C语言学习之路 Day 6: 今天,我们要开始学习数组了. //①数组部分,数组的大小不能够动态定义.如下: //int n; scanf("%d,& ...
随机推荐
- Castle ActiveRecord框架学习(一)
一.Active Record(活动记录)模式 Active Record是业务逻辑层中(<企业应用架构模式>将该模式归为数据源模式)常用的一种框架模式,尤其在底层数据库模型匹配业务模型时 ...
- Asp.net中使用Server.HtmlDecode(string str)的使用
前言: 在使用Visual Studio开发web页面时,需要在GridView中绑定Table数据,并加入了CommandField, 试图,点击详情按钮是,获取GridView中Rows中Cell ...
- 【C#】第2章学习要点
分类:C#.VS2015 创建日期:2016-06-15 教材:(十二五国家级规划教材)<C#程序设计及应用教程>(第3版) 一.本章要点 C#数据类型分两大类:值类型.引用类型. 值类型 ...
- Firemonkey 载入 Style 皮肤 (*.fsf 二进制文件) 速度测试
说明:Firemonkey 可以换肤是一大亮点,但使用它必须要付出一点代价,就是需要一点载入的时间,下面以 *.fsf 二进制文件来做载入测试,有兴趣可以参考看看. 开发:XE8 for iOS 皮肤 ...
- mysql学习笔记 第五天
使用分区数据表: 分区数据表和merge数据表具有相似的作用,但是分区数据表确确实实是一个数据表 ,不像merge是列出数据表的逻辑关系,并且分区数据表可以包括像myisam以外的 的数据表.创建分区 ...
- Git分支(本地)
1.Git保存的不是文件的差异或者变化量,而只是一系列文件快照(File Snapshot): 2.暂存操作会对每一个文件计算校验和(SHA-1哈希字符串),然后把当前版本的文件快照保存到Git仓 ...
- 重新初始化 VS2010
开始->所有程序->Microsoft Visual Stdio 2005->Visual Stdio Tools->Visual Stdio 2005 命令提示 这时会弹出一 ...
- web桌面程序之图标拖动排序的分析
在web桌面程序里,图标拖动并重新排序是个比较常见的功能.这个功能我之前反复修改了好几遍,现在终于整理出了比较理想的解决思路,决定拿出来分享下. 这一功能主要有哪些难点呢?我总结了一下一共有2处难点: ...
- 免费的 Photoshop Apple Watch 原型设计素材
大量的扁平化的苹果设备原型展示了响应式的 Web 设计.这是一组免费的 Photoshop Apple Watch 原型 PSD 设计素材,文件包括 iPhone.iPad. iMac 和 Macbo ...
- 如何停止CSS3的动画?
前言 我们在移动端一般使用zepto框架,与其说zepto是jquery的轻量级替代版,不如说是html5替代版我们在js中会用到animate方法执行动画,这个家伙可是真资格的动画,完全是css一点 ...