asp.net-mvc 如果用户在 int 字段中放置非数字字符串,则自定义验证错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7351206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom validation error message if user puts a non-numeric string in an int field
提问by Jeremy Holovacs
This question has to have been asked before, but I think the search terms are too generic for me to find the answer I'm looking for, so I'll ask it again.
这个问题之前肯定有人问过,但我认为搜索词太笼统了,我无法找到我要找的答案,所以我会再问一次。
I have a model with an intproperty, and a range annotation.
我有一个带有int属性和范围注释的模型。
If the user enters something other than an int, the validation message responds with The value '<bad data>' is not valid for '<property name>'... which is great, but I want to provide a bit more feedback, i.e., Expecting an integer value in this field..
如果用户输入 int 以外的内容,则验证消息会以The value '<bad data>' is not valid for '<property name>'...响应,这很好,但我想提供更多反馈,即Expecting an integer value in this field..
Since this validation fails before the other validators take a look, I don't know how (or if it's possible) to override the default validator message for this.
由于此验证在其他验证器查看之前失败,因此我不知道如何(或是否可能)为此覆盖默认验证器消息。
What are my options?
我有哪些选择?
per request, I am posting the code, but there's not a lot to it:
根据请求,我发布了代码,但内容不多:
[Range(0,65535, ErrorMessage="Port must be between 0 and 65535.")]
public int Port { get; set; }
There is validation that occurs before it reaches the RangeAttribute. I want to replace the default message with one of my own choosing.
在到达 RangeAttribute 之前会发生验证。我想用我自己选择的消息替换默认消息。
采纳答案by Cymen
If you're using standard annotations, you should be able to override the error message with something like this:
如果您使用标准注释,您应该能够使用以下内容覆盖错误消息:
[MyAnnotation(...., ErrorMessage = "My error message")]
public int myInt { get; set; }
Or do you actually want to append to the default error message instead of replacing it (not clear in question)?
或者你真的想附加到默认错误消息而不是替换它(不清楚)?
Update: Misread -- suggest this as the answer: How to change the ErrorMessage for int model validation in ASP.NET MVC?or better yet How to change 'data-val-number' message validation in MVC while it is generated by @Html helper
更新:误读 - 建议将此作为答案:如何更改 ASP.NET MVC 中 int 模型验证的 ErrorMessage?或者更好的是如何在 MVC 中更改“data-val-number”消息验证,而它是由 @Html 助手生成的
回答by alok_dida
You can also inherit IValidatableObject in your model class. You can write down your required logic in the Validate method. Please find sample code below.
您还可以在模型类中继承 IValidatableObject。您可以在 Validate 方法中写下所需的逻辑。请在下面找到示例代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace MvcApplication1.Models
{
public class Alok : IValidatableObject
{
[Display(Name = "Property1")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Property1 is required.")]
public int Property1 { get; set; }
[Display(Name = "Property2")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Property2 is required.")]
public int Property2 { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Property1 < Property2)
{
yield return new ValidationResult("Property 1 can't be less than Property 2.");
}
}
}
}
回答by Iridio
read this question. In the link that the OP suggest you will find the way to replace the deafult error string that use the framework, while in the answer you will find a linnk to the other resources in case you want to change all of them. Look also here. Hope it helps
阅读这个问题。在 OP 建议的链接中,您将找到替换使用框架的默认错误字符串的方法,而在答案中,您将找到指向其他资源的链接,以防您想更改所有资源。也看这里。希望能帮助到你

