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

时间:2018-07-22 14:43:33

标签: c# validation datetime asp.net-mvc-5

我的剃刀中有一个字段:

<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>

当前它具有一些验证,因此该字段不能留为空白。但是我想添加一些验证。基于模型中另一个称为终止日期的字段。

我已经在模型中尝试过此操作

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

但是它并没有真正按预期工作。它不会带回验证消息。我在想可能需要自定义验证器属性。

型号:

    #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; }
    }

1 个答案:

答案 0 :(得分:0)

最终添加

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

进入模型,然后为Termination Date添加一个表单字段。然后,我选择隐藏该字段,因为它不需要显示。然后,我检查了验证是否可以在两种情况下使用。

    <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>