简体   繁体   English

检查日期大于C#ASP.NET MVC

[英]Check Date is Greater Than C# ASP.NET MVC

I have a field in my Razor: 我的剃刀中有一个字段:

<div class="form-group">
    @Html.LabelFor(m => m.CreateContractStep1.StartDate, new { @class = "control-label col-md-2" })
    <div class="col-md-4">
        <div class="admin-form theme-primary">
            <label for="StartDate" class="field prepend-icon mbn">
                @Html.TextBoxFor(m => m.CreateContractStep1.StartDate, new { id = "StartDate", @class = "gui-input datepicker", placeholder = "From...", data_bind = "value: CreateContractStep1.StartDate" })
                @Html.ValidationMessageFor(m => m.CreateContractStep1.StartDate, string.Empty, new { @class = "text-danger fw400" })
                <label class="field-icon">
                    <i class="fa fa-calendar-o"></i>
                </label>
            </label>
        </div>
    </div>
</div>

Currently it has some validation present so that the field cannot be left blank. 当前,它具有一些验证,因此该字段不能留为空白。 However I would like to add some validation. 但是我想添加一些验证。 Based on another field called Termination Date which is in the Model. 基于模型中另一个称为终止日期的字段。

I have tried this in my Model: 我已经在我的模型中尝试过:

 [Display(Name = "From")]
 [Required]
 [GreaterThan("TerminationDate", true, "Termination date must be greater than or equal to Notification Date")]
 public DateTime? StartDate { get; set; }

But it's not really working as expected. 但是它并没有真正按预期工作。 It does not bring back the Validation Message. 它不会带回验证消息。 I am thinking that I may need a Custom Validator Attribute. 我在想可能需要自定义验证器属性。

Model: 模型:

    #region Create Contract Step 1
    public class ContractStep1ViewModel
    {
        public ContractStep1ViewModel()
        {
            // Default named type to contract.
            NamedType = ContractNamedType.Contract;
        }

        [Display(Name = "Name *")]
        [Required]
        [StringLength(250, ErrorMessage = "The {0} has a maximum of {1} characters.")]
        public string Name { get; set; }

        [Display(Name = "Description")]
        [StringLength(1000, ErrorMessage = "The {0} has a maximum of {1} characters.")]
        public string Description { get; set; }

        [Display(Name = "Contract Type *")]
        [Required]
        public ContractType? ContractType { get; set; }

        [Display(Name = "Contract Or Sideletter *")]
        [Required]
        public ContractNamedType NamedType { get; set; }

        [Display(Name = "Currency Used *")]
        [Required]
        public string Currency { get; set; }

        [Display(Name = "From *")]
        [Required]
        [Compare("End Date")]
        [DataType(DataType.DateTime)]
        public DateTime? StartDate { get; set; }

        [Display(Name = "To")]
        [GreaterThan("StartDate", true, "End Date must be greater than or equal to Start Date")]
        public DateTime? EndDate { get; set; }

        [Display(Name = "Signed")]
        public DateTime? SignedDate { get; set; }
    }

 public class TerminateContractViewModel
    {
        [Required]
        public Guid ContractId { get; set; }

        [Required]
        public Guid RowVersion { get; set; }

        [Display(Name = "Name")]
        [Required]
        public string Name { get; set; }

        [Required]
        public DateTime ContractStartDate { get; set; }

        [Display(Name = "Notification Date")]
        [Required]
        [GreaterThan("ContractStartDate", false, "Notification date must be greater than or equal to Contract Start Date")]
        public DateTime NotificationDate { get; set; }

        [Display(Name = "Termination Date")]
        [Required]
        [GreaterThan("NotificationDate", true, "Termination date must be greater than or equal to Notification Date")]
        public DateTime TerminationDate { get; set; }

        [Display(Name = "Post Term Collection End Date")]
        [Required]
        [GreaterThan("TerminationDate", true, "Post term collection date must be greater than or equal to Termination Date")]
        public DateTime PostTermCollectionEndDate { get; set; }

        // search
        public ContractSearchViewModel SearchModel { get; set; }
        // pagination
        public PagingModel PagingInfo { get; set; }
    }

Ended up adding 最终添加

[Display(Name = "Termination Date")]
public DateTime? TerminationDate { get; set; }

Into the Model and then adding in a Form Field for the Termination Date . 进入模型,然后为Termination Date添加一个表单字段。 I then chose to hide the field as it was not required to be shown. 然后,我选择隐藏该字段,因为它不需要显示。 I then checked that the validation worked with a couple of scenarios. 然后,我检查了验证是否可以在两种情况下使用。

    <div class="form-group hidden-xs hidden-sm hidden-md hidden-lg">
    @Html.LabelFor(m => m.CreateContractStep1.TerminationDate, new { @class = "control-label col-md-2" })
    <div class="col-md-4">
    <div class="admin-form theme-primary">
    <label for="SignedDate" class="field prepend-icon mbn">
    @Html.TextBoxFor(m => m.CreateContractStep1.TerminationDate, new { id = "TerminationDate", @class = "gui-input datepicker", placeholder = "Termination Date (Optional)...", data_bind = "value: CreateContractStep1.TerminationDate" })
 @Html.ValidationMessageFor(m => m.CreateContractStep1.TerminationDate, string.Empty, new { @class = "text-danger" })
<label class="field-icon">
<i class="fa fa-calendar-o"></i>
</label>
</label>
</div>
</div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM