int或数数据类型DataAnnotation验证特性 [英] Int or Number DataType for DataAnnotation validation attribute

查看:430
本文介绍了int或数数据类型DataAnnotation验证特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC3项目,我保存了足球/足球/曲棍球/ ...运动游戏分数prediction。所以,我的prediction类的属性之一是这样的:

On my MVC3 project, I store score prediction for football/soccer/hockey/... sport game. So one of properties of my prediction class looks like this:

[Range(0, 15, ErrorMessage = "Can only be between 0 .. 15")]
[StringLength(2, ErrorMessage = "Max 2 digits")]
[Remote("PredictionOK", "Predict", ErrorMessage = "Prediction can only be a number in range 0 .. 15")]
public int? HomeTeamPrediction { get; set; }

现在,我也需要更改错误信息在我的情况下,数据类型, INT 。目前使用的一些默认的 - 这个领域HomeTeam prediction必须是一个数字。需要找到一种方法如何改变这种错误消息。这验证消息似乎也采取远程验证1 prediction。

Now, I need also change error message for a data type, int in my case. There is some default one used - "The field HomeTeamPrediction must be a number.". Need to find a way how to change this error message. This validation message also seem to take prediction for Remote validation one.

我试过 [数据类型] 属性,但是这似乎并没有在 system.componentmodel.dataannotations.datatype 枚举。

I've tried [DataType] attribute but this does not seem to be plain number in system.componentmodel.dataannotations.datatype enumeration.

谢谢,

安东尼

推荐答案

有关任何数字验证你必须使用不同的不同的范围验证按您的要求:

For any number validation you have to use different different range validation as per your requirements :

有关整数

[Range(0, int.MaxValue, ErrorMessage = "Please enter valid integer Number")]

浮法

[Range(0, float.MaxValue, ErrorMessage = "Please enter valid float Number")]

 [Range(0, double.MaxValue, ErrorMessage = "Please enter valid doubleNumber")]

这篇关于int或数数据类型DataAnnotation验证特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆