asp.net-mvc-3 – 大于或等于今天日期验证注释在MVC3
发布时间:2020-12-16 00:33:53 所属栏目:asp.Net 来源:网络整理
导读:有没有人看到一个MVC3数据注释的日期验证,需要一个选定的日期等于或大于当前日期? 如果已经有第三方添加了很酷。我已经在使用DataAnnotationsExtensions,但不提供我正在寻找的内容。 这似乎没有任何参考。所以,希望有人已经解决了这个问题,然后再尝试重
有没有人看到一个MVC3数据注释的日期验证,需要一个选定的日期等于或大于当前日期?
如果已经有第三方添加了很酷。我已经在使用DataAnnotationsExtensions,但不提供我正在寻找的内容。 这似乎没有任何参考。所以,希望有人已经解决了这个问题,然后再尝试重新创造轮子,并写下自己的定制验证器。 我已经尝试了Range,但需要2个日期,并且都必须是字符串格式的常量,例如[Range(typeof(DateTime),“1/1/2011”,“1/1/2016”)]帮助。而DataAnnotationsExtensions Min验证器只接受int和double 更新已解决 感谢@BuildStarted这是我结束了,它的工作伟大的服务器端,现在客户端与我的脚本 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace Web.Models.Validation { [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,AllowMultiple = false,Inherited = true)] public sealed class DateMustBeEqualOrGreaterThanCurrentDateValidation : ValidationAttribute,IClientValidatable { private const string DefaultErrorMessage = "Date selected {0} must be on or after today"; public DateMustBeEqualOrGreaterThanCurrentDateValidation() : base(DefaultErrorMessage) { } public override string FormatErrorMessage(string name) { return string.Format(DefaultErrorMessage,name); } protected override ValidationResult IsValid(object value,ValidationContext validationContext) { var dateEntered = (DateTime)value; if (dateEntered < DateTime.Today) { var message = FormatErrorMessage(dateEntered.ToShortDateString()); return new ValidationResult(message); } return null; } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context) { var rule = new ModelClientCustomDateValidationRule(FormatErrorMessage(metadata.DisplayName)); yield return rule; } } public sealed class ModelClientCustomDateValidationRule : ModelClientValidationRule { public ModelClientCustomDateValidationRule(string errorMessage) { ErrorMessage = errorMessage; ValidationType = "datemustbeequalorgreaterthancurrentdate"; } } } 在我的模特儿 [Required] [DateMustBeEqualOrGreaterThanCurrentDate] public DateTime SomeDate { get; set; } 客户端脚本 /// <reference path="jquery-1.7.2.js" /> jQuery.validator.addMethod("datemustbeequalorgreaterthancurrentdate",function (value,element,param) { var someDate = $("#SomeDate").val(); var today; var currentDate = new Date(); var year = currentDate.getYear(); var month = currentDate.getMonth() + 1; // added +1 because javascript counts month from 0 var day = currentDate.getDate(); var hours = currentDate.getHours(); var minutes = currentDate.getMinutes(); var seconds = currentDate.getSeconds(); today = month + '/' + day + '/' + year + ' ' + hours + '.' + minutes + '.' + seconds; if (someDate < today) { return false; } return true; }); jQuery.validator.unobtrusive.adapters.addBool("datemustbeequalorgreaterthancurrentdate"); 解决方法
创建自定义属性。
public class CheckDateRangeAttribute: ValidationAttribute { protected override ValidationResult IsValid(object value,ValidationContext validationContext) { DateTime dt = (DateTime)value; if (dt >= DateTime.UtcNow) { return ValidationResult.Success; } return new ValidationResult(ErrorMessage ?? "Make sure your date is >= than today"); } } 代码被注销袖口,以便修复任何错误:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET AJAX AsyncFileUpload UploadedComplete不会触发
- 如果ASP.NET应用程序的CustomErrors设置为Off,有没有办法以
- 【译】使用Jwt身份认证保护 Asp.Net Core Web Api
- asp.net-mvc – ASP.NET MVC – Html.Textbox()throws“对象
- C#使用Log4Net记录日志
- DxPackNet 5.视频高质量的压缩和传输
- asp.net – 返回按钮刷新页面
- Linq与ASP.NET MVC 3中的MySQL数据库,将DateTime存储到变量
- asp.net-mvc-3 – MVC3 – 无法通过其他操作将int []传递给
- 将我的思维模式从ASP.NET迁移到ASP.NET MVC(2)时需要了解哪
推荐文章
站长推荐
- asp.net-mvc-4 – 如何在asp.net mvc应用程序中使
- asp.net-mvc – MVC4捆绑通配符 – *与{version}
- asp.net-mvc – 具有不同参数名称的Asp.Net路由
- asp.net – 如何以编程方式设置Elmah电子邮件的收
- “前.NET Core时代”如何实现跨平台代码重用 ——
- asp.net – 如何指定相对于站点根目录的路径?
- asp.net – Azure网站上HTTP 1.0代理请求的gzip压
- asp.net-mvc – 具有键’XXX’的ViewData项的类型
- asp.net-mvc – 特定便携式区域的ControllerFact
- asp.net-web-api – 使用OWIN SelfHost和Windows
热点阅读