数据注释日期范围
是否可以对日期使用 [Range]
注释?
类似的东西
[Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.Today.ToString())]
Is it possible to use [Range]
annotation for dates?
something like
[Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.Today.ToString())]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我这样做是为了解决你的问题
I did this to fix your problem
MSDN 上的文档 说您可以使用 RangeAttribute
Docs on MSDN says you can use the RangeAttribute
jQuery 验证不适用于
[Range(typeof(DateTime),"date1","date2"]
--我的 MSDN 文档不正确。
jQuery validation does not work with
[Range(typeof(DateTime),"date1","date2"]
--My MSDN doc is incorrect.
这是另一个解决方案。
只需创建一个名为 ValidationController 的新 MVC 控制器并将此代码放入其中即可。 “远程”方法的好处是您可以利用此框架根据您的自定义逻辑处理任何类型的验证。
Here is another solution.
The simply create a new MVC controller called ValidationController and past this code in there. The nice thing about the "Remote" approach is you can leverage this framework to handle any kind of validations based on your custom logic.
对于那些罕见的情况,当您被迫将日期写为字符串时(使用属性时),我强烈建议使用 ISO-8601 表示法。
这就消除了关于 01/02/2004 是 1 月 2 日还是 2 月 1 日的任何混淆。
For those rare occurrences when you are forced to write a date as a string (when using attributes), I highly recommend using the ISO-8601 notation.
That eliminates any confusion as to whether 01/02/2004 is january 2nd or february 1st.
我使用这种方法:
并像这样使用它:
I use this approach:
And use it like so:
我发现了
[Range(typeof(DateTime)]
注释的问题,并将其描述为“充其量是笨重的”,如果它有效的话,它留下了太多的机会。远程验证似乎是一个很好的方法:避免在视图中使用 JavaScript 并维护服务器端代码的完整性,如果我可以避免的话,我个人从不喜欢将代码发送到客户端来执行
。 aaronstannard.com/remote-validation-asp-net-mvc3/" rel="nofollow noreferrer">关于 MVC3 中远程验证的文章
模型
控制器 - 部署在根目录
msg
变量显示为 Html 帮助器 ValidationSummary 或 Html 帮助器 ValidationFor(x=>x.DATETIME)View 的
一部分,值得注意的是作为参数 2 和 3 传递的字段必须存在于视图中,以便远程验证将值传递到控制器:
模型和 Html 帮助程序将为您完成所有 jquery 工作。
I found issues with the
[Range(typeof(DateTime)]
annotation and would describe it as "clunky at best" it leaves too much to chance IF it works.Remote validation seems to be a good way of: avoiding javascript in views and maintaining server side code integrity, personally never like sending code to a client to execute if I can avoid it.
Using @StackThis answer as a base and reference to an article on remote validation in MVC3
Model
Controller - Deployed at the root directory
The
msg
variable is displayed as part of the Html helper ValidationSummary or the Html helper ValidationFor(x=>x.DATETIME)View
It's important to note that the fields passed as parameter 2 and 3 must exist in the view in order for the remote validation to pass the values to the controller:
The model and Html helpers will do all the jquery work for you.