C# ASP.NET Mvc警告消息(如果选择的日期为过去日期)

C# ASP.NET Mvc警告消息(如果选择的日期为过去日期),c#,jquery,datepicker,C#,Jquery,Datepicker,在我的mvc web应用程序中,我有一个员工用来提交假期请求的表单。如果选择了过去的日期,是否有办法显示警告消息?类似于验证消息,但我仍然希望员工能够选择过去的日期 以下是我在视图中的表单: @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal" style=" position:relative; top:20px;border-radius: 0px;

在我的mvc web应用程序中,我有一个员工用来提交假期请求的表单。如果选择了过去的日期,是否有办法显示警告消息?类似于验证消息,但我仍然希望员工能够选择过去的日期

以下是我在视图中的表单:

     @using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal" style=" position:relative; top:20px;border-radius: 0px; border-color: #F47B20; border-style: solid; border-width: 5px; background-repeat: no-repeat; background-position: right; padding: 60px; background-size: contain; background-color:white ">
        <h2 align="center">Holiday Request Form</h2>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.StartDate, "Start Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StartDate, "Start Date", new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
                @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.FinishDate, "Finish Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FinishDate, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
                @Html.ValidationMessageFor(model => model.FinishDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HoursTaken, "Hours Requested", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HoursTaken, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HoursTaken, "", new { @class = "text-danger" })
            </div>
        </div>




        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Submit" class="btn btn-warning" />
            </div>
        </div>
    </div>
}

@section Scripts {
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/cssjqryUi")

<script type="text/javascript">

    $(document).ready(function () {
        $('input[type=datetime]').datepicker({
            dateFormat: "dd/M/yy",
            changeMonth: true,
            changeYear: true,
            yearRange: "-70:+70"
        });

    });
</script>

您可以使用jQuery完成所有工作。代码如下:

HTML:


您可以为此情况创建自定义属性

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class CheckDateAttribute : ValidationAttribute
    {   
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            DateTime date = (DateTime)value;


            if (date > DateTime.Now)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult(ErrorMessage);
            }
        }
    }
并设置为您的属性:

[CheckDate(ErrorMessage = "Date should not past")]
        public string StartDate { get; set; }
如果你不想改变你的模型类,你可以这样做

public class EmployeeMetaData
    {

        [CheckDate(ErrorMessage = "Date should not past")]
        public string StartDate { get; set; }
    }

    [MetadataType(typeof(EmployeeMetaData))]
    public partial class Employee
    {

    }

在JQuery中捕获开始日期和结束日期选择,然后在客户端上显示一个弹出窗口if在过去。只需使用if条件进行简单的日期比较就足够了。根据您用来显示日期选择器的库,您可以尝试对此问题的建议:。太好了,它可以工作!!您能解释一下如何使用表单字段/editorfor实现这个功能吗?是的,非常简单。在下面的ValidationMessageFor中添加一段,就像我在上面的示例中添加的那样

,并将我在上面编写的jQuery代码放入表单末尾的标记中。取而代之的是$datepicker,你有$StartDate,这样你的字段ID就好了,太好了。我还有一个日期选择器,我把它添加到了问题中。这是放在一个单独的函数中还是放在同一个函数中?你可以放在同一个函数中。只需选择,功能日期在年范围:-70:+70之后。是的,工作完美!谢谢,这不会阻止输入过去的日期。我希望它只显示一个警告,但用户仍然可以输入过去的日期您可以从类文本危险更改为文本警告它仍然阻止表单提交?
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class CheckDateAttribute : ValidationAttribute
    {   
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            DateTime date = (DateTime)value;


            if (date > DateTime.Now)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult(ErrorMessage);
            }
        }
    }
[CheckDate(ErrorMessage = "Date should not past")]
        public string StartDate { get; set; }
public class EmployeeMetaData
    {

        [CheckDate(ErrorMessage = "Date should not past")]
        public string StartDate { get; set; }
    }

    [MetadataType(typeof(EmployeeMetaData))]
    public partial class Employee
    {

    }