针对MVC非必需的属性整数验证 [英] Integer validation against non-required attributes in MVC
问题描述
我试图验证模型上我有一个属性。此属性不是必需的,所以如果无效MVC似乎忽略它。我甚至创建的自定义ValidationAttribute,但没有任何工程。
I've trying to validate a property on a model I have. This property is NOT required, and so if its invalid MVC seems to be ignoring it. I've even created a custom ValidationAttribute, but nothing works.
public class NumberWang : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value == null)
return true;
int g;
if (int.TryParse(value.ToString(), out g))
{
if (g >= 0)
return true;
}
return false;
}
}
public class MyModel
{
[Range(0, 999999, ErrorMessage = "category_id must be a valid number")]
[NumberWang(ErrorMessage = "That's NumberWang!")]
public int? category_id { get; set; }
/* there are other properties of course, but I've omitted them for simplicity */
public void Validate()
{
Validator.TryValidateProperty(this.category_id,
new ValidationContext(this, null, null) { MemberName = "category_id" },
this.validation_results);
}
}
如果我传递值'ABC'作为CATEGORY_ID这个模型,它验证就好了。我究竟做错了什么?
If I pass the value 'abc' as a category_id to this model, it validates just fine. What am I doing wrong?
推荐答案
我发现了一个丑陋的解决方法。
I found an ugly workaround.
看来,如果CATEGORY_ID是一个可空 INT?
和我的价值是不是一个有效的数字,一个null值传递,该模型并没有看到无效ABC的价值。
It seems that if category_id is a nullable int?
and my value is not a valid number, a null value is passed, and the model doesn't see the invalid 'abc' value.
[Range(0, 999999, ErrorMessage = "category_id must be a valid number")]
public int? category_id { get; set; }
// when we pass a good number
MyAction?category_id=123
validation: successful
// when we pass a bad number
// validation ignores it. not what we want.
MyAction?category_id=abc
validation: successful
如果我更改为CATEGORY_ID一个非空的 INT
,验证失败,即使没有值传递。
If I change category_id to a non-nullable int
, it fails validation even when no value is passed.
[Range(0, 999999, ErrorMessage = "category_id must be a valid number")]
public int? category_id { get; set; }
// when we pass a good number
MyAction?category_id=123
validation: successful
// when we pass an bad number
MyAction?category_id=abc
validation: "category_id must be a valid number"
// BUT, when we don't pass any number at all ...
MyAction
validation: "category_id must be a valid number"
丑陋的解决方法
如果我改变CATEGORY_ID到字符串
,然后只将它转换为 INT
,当我需要的时候,我可以正确验证它,只用 [范围]
If I change category_id to a string
, and then only convert it to an int
when I need it, I can validate it properly, using only [Range]
[Range(0, 999999, ErrorMessage = "category_id must be a valid number")]
public string category_id { get; set; }
// when we pass a good number
MyAction?category_id=123
validation: successful
// when we pass a bad number
MyAction?category_id=abc
validation: "category_id must be a valid number"
// no number, no validation. hooray!
MyAction
validation: successful
这是丑陋的,但它的作品。
It's ugly, but it works.
(注意:没有必要自定义属性,所以我删除它,只是使用[范围])
(Note: the custom attribute was not needed, so I removed it and just used [Range])
这篇关于针对MVC非必需的属性整数验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!