为什么 ASP.Net MVC Range 属性采用类型?
我只是想知道为什么范围验证属性可以采用类型和两个字符串作为参数?这是为了根据枚举或类似的东西验证字符串吗?
另外,我想做的是找到一种简单的方法来验证必须出现在枚举中的 3 个字符的字符串,有什么建议吗?
谢谢, 亚历克斯.
I was just wondering why the Range validation attribute can take a Type and two strings as parameters? Is this for validating strings against an Enum or something like this?
Also what I am trying to do is find an easy way to validate a 3 character string which must be present in an enum, any sugestions?
Thanks,
Alex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我确实发现你提到的 Range ctor 很可疑。忍不住去调查一下。 (所以我在调查时像日志一样写了这个答案。)
来自 MSDN
注意:MSDN 说
Type
应该是IComparable
。而且,他们的示例描述说它用于日期比较,但事实并非如此!因此,自从我打开了生产 asp.net mvc3 应用程序后,我在这样的日期时间上尝试了此操作:
当我运行它时,会发生这种情况:
请注意,虽然我用虚线指定了最小值和最大值,但没有时间,但它给出了不同的格式,所以它可能
TryParsing
字符串,对吗?但我确信它不可能在客户端ICompare
这两者!?现在无论我输入什么日期仍然显示错误。 (日期输入为 11-Mar-20(2020 年为 20)。)我尝试了 char(如下),因为那也是一个
IComparable
。同样的事情。它实际上无法在客户端进行范围比较。但是等等...
只需删除客户端验证即可!我删除了对 JQuery 验证和 Unobtrusive 验证以及 viola 的引用!它工作完美。当值(字符和日期)不在指定范围内时,它发布,然后正确显示错误。
注意:也许有人可以扩展此解决方案以仅禁用某些字段进行客户端验证。
希望这有帮助。
I did find the Range ctor you mentioned fishy. Couldn't help but investigate. (So I wrote this answer like a log while investigating.)
From MSDN
Note : MSDN says the
Type
should beIComparable
. And, their example description says its for a date comparison when its not!.So since I had my production asp.net mvc3 app opened I tried this on a with a date time like this:
When I run it this happens:
Note how although I specified the minimum and maximum with dashed and no time, it gives a different format, so its probably
TryParsing
the strings right? But I'm sure it can't possiblyICompare
the two on the client side!? Now no matter what date I enter still shows the error. (The Date is entered as 11-Mar-20 (20 as in 2020).)I tried char (below), since thats an
IComparable
too. Same thing. It can't actually manage to do a range comparison on the client side.But wait...
Just remove Client Validation! I Removed References to JQuery validation and Unobtrusive validation and viola! It works perfect. It posts, then shows the errors correctly when the values (Both Char and Date) are NOT within the specified range.
Note: Maybe someone can expand this solution to disabling ONLY certain fields for client validation.
Hope this was helpful.
我还注意到 jQuery 验证与 ASP MVC 范围验证器配合得不好(似乎 jQuery 验证插件要求范围值是数字)。
一个简单的解决方案是关闭特定字段的验证。无论如何,服务器端验证都会起作用。
以下示例将从所有具有“date”类的输入字段中删除“range”规则:
I've also noticed that jQuery Validation does not play well with the ASP MVC Range-validator (it seems like jQuery Validation plugin requires the range values to be numbers).
A simple solution is to turn off validation for the specific fields. The server side validaton will work anyway.
The following example will remove the "range" rule from all input fields with a "date" class:
我最终创建了一个自定义 DateRangeAttribute,如此处所述。
您不会获得客户端验证,但您可以对其进行自定义以满足您的需求,并且不需要弄乱 JavaScript。这是代码以及如何使用它:
DateRange 很简单:
I ended up creating a custom DateRangeAttribute as described here.
You don't get client side validation, but you can customize this to fit your needs and no messing with javascript required. Here's the code and how to use it:
DateRange is simply:
我也很晚才意识到这一点:)
CustomValidation 属性肯定是针对这些情况构建的吗?
这样我们就不必去改变任何客户端验证。另外,它的优势在于它使我们有机会应用可配置的范围。
例如:
I too have picked up on this fairly late :)
Surely the
CustomValidation
attribute was built for these circumstances?This way we don't have to go about altering any client side validation. Plus it is advantageous in that it allows us the opportunity to apply a configurable range.
For example:
您可以使用以下自定义日期范围验证,将日期值与提供的最小和最大日期或提到的依赖属性进行比较。它还演示了客户端验证支持和集成。
自定义日期范围
客户端集成
演示
模型
带验证的日期控件
从 此处。
You can use the below custom date range validation which compares the date value to provided min and max date or mentioned dependent properties. It also demonstrates the client side validation support and integration.
CustomDateRange
ClientSide Integration
Demo
Model
Date controls with validations
Download the complete implementation from here.
伙计们!这里还有一个问题。
我在这里找到了它:
MVC 验证低于/高于其他值
http://foolproof.codeplex.com/
Guys! Here is one more solition.
I found it here:
MVC Validation Lower/Higher than other value
http://foolproof.codeplex.com/