什么是使用MVC和数据注解的客户端将大于0的验证程序的最佳方式? [英] What is the best way of adding a greater than 0 validator on the client-side using MVC and data annotation?

查看:127
本文介绍了什么是使用MVC和数据注解的客户端将大于0的验证程序的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够只允许一个表单提交,如果在某个字段中的值大于0。我想也许MVC的范围属性,让我进入只有1值表示只有大于a测试,但没有运气,因为它坚持最小值和最大值。

I'd like to be able to only allow a form to submit if the value in a certain field is greater than 0. I thought maybe the Mvc Range attribute would allow me to enter only 1 value to signify only a greater than test, but no luck there as it insists on Minimum AND Maximum values.

任何想法如何实现这一点?

Any ideas how this can be achieved?

推荐答案

您不能存储比我的大了一些你的基础数据类型可以容纳这样一个事实,即范围属性需要一个最大值是一件非常好的事情。请记住,不会在现实世界中存在,所以下面应该工作:

You can't store a number bigger than what your underlying data type could hold so that fact that the Range attribute requires a max value is a very good thing. Remember that doesn't exist in the real world, so the following should work:

[Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")]
public int Value { get; set; }

这篇关于什么是使用MVC和数据注解的客户端将大于0的验证程序的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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