如何更改所有int字段的消息,而不是说:
The field must be a number 用英文表示:
El campo tiene que ser numerico 在西班牙语中.
有办法吗?
validation asp.net-mvc localization numbers data-annotations
App_GlobalResources使用PropertyValueRequired密钥添加资源文件并更改DefaultModelBinder.ResourceClassKey为文件名对MVC 4没有影响.字符串The {0} field is required永远不会更改.我不想在每个必填字段上设置资源类类型和键.我错过了什么吗?
编辑:
我对Darin Dimitrov的代码进行了一些小修改,以保持必需的自定义工作:
public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
public MyRequiredAttributeAdapter(
ModelMetadata metadata,
ControllerContext context,
RequiredAttribute attribute
)
: base(metadata, context, attribute)
{
if (attribute.ErrorMessageResourceType == null)
{
attribute.ErrorMessageResourceType = typeof(Messages);
}
if (attribute.ErrorMessageResourceName == null)
{
attribute.ErrorMessageResourceName = "PropertyValueRequired";
}
}
}
Run Code Online (Sandbox Code Playgroud) 这问题之前已经被问过了,但是我认为搜索条件对于我来说太通用了,无法找到我正在寻找的答案,所以我会再问一遍.
我有一个带有int属性的模型和范围注释.
如果用户输入的不是int,则验证消息会响应The value '<bad data>' is not valid for '<property name>'...这很好,但我想提供更多反馈,即Expecting an integer value in this field..
由于此验证在其他验证器查看之前失败,因此我不知道如何(或者是否可能)覆盖默认的验证器消息.
我有什么选择?
每个请求,我发布的代码,但它没有很多:
[Range(0,65535, ErrorMessage="Port must be between 0 and 65535.")]
public int Port { get; set; }
Run Code Online (Sandbox Code Playgroud)
在到达RangeAttribute之前进行验证.我想用我自己选择的替换默认消息.
我正在尝试本地化数据注释的验证消息.我认为可以按照此处所述完成:使用非英语语言环境支持ASP.NET MVC 3验证.
现在它说ASP.NET MVC和System.ComponentModel.DataAnnotations命名空间中的类型使用它们自己的本地化消息.那对我来说或多或少都是无用的,只是帮助格式化例如价格?
但回到真正的问题,所以本地化验证消息的唯一方法是做这样的事情? 在mvc 2中本地化默认模型验证
只是想在这里得到一些澄清,谢谢=)
我坚持使用ASP.NET MVC 3 jQuery不显眼的验证消息本地化.特别是"数字"验证.如果我在模型输入中有一个数字属性html使用data-val-number属性进行渲染,其值为"字段数量必须是数字".我如何本地化这个字符串.使用数据注释属性,定义本地化消息没有问题.但是对于数字验证,我不必指定任何属性.
那么,如何通过不引人注意的验证生成本地化验证消息呢?
我收到The value 'abc' is not valid for fieldName.错误消息.这是默认的错误消息,我想以更简单的方式覆盖它.
截至目前我所尝试的内容如下所示
[RegularExpression(@"^\d+$",ErrorMessage="enter numeric value")][Integer(ErrorMessageResourceType = typeof(appName.Resources.abc.Resource),
ErrorMessageResourceName = "error_numeric")][RegularExpression("([1-9][0-9]*)")]Range(1,int.max,ErrorMessage="enter numeric value")
但无法更改默认错误消息.
建议我做最简单的方法.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace blueddPES.ViewModels
{
public class ContactViewModel
{
[Integer(ErrorMessage="sdfdsf")]
public int? hp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)