相关疑难解决方法(0)

大于或等于今天在MVC3中的日期验证注释

有没有人看过日期验证的MVC3数据注释要求单个选定日期等于或大于当前日期?

如果已经有第三方添加,那也很酷.我已经在使用DataAnnotationsExtensions,但它没有提供我正在寻找的东西.

似乎没有任何关于此的参考.因此,希望有人在我尝试重新发明轮子并编写自己的自定义验证器之前已经解决了这个问题.

我已经尝试Range但需要2个日期,两者都必须是字符串格式的常量,[Range(typeof(DateTime), "1/1/2011", "1/1/2016")]但这没有帮助.DataAnnotationsExtensions Min验证器只接受intdouble


更新已解决

感谢@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 …
Run Code Online (Sandbox Code Playgroud)

datetime data-annotations asp.net-mvc-3

25
推荐指数
2
解决办法
4万
查看次数

标签 统计

asp.net-mvc-3 ×1

data-annotations ×1

datetime ×1