I am implementing eonasdan's datepicker in asp.net. The field VisitDate is not required in the SQL DB so I want to allow the user to leave it empty if desired. If it is not empty, I want to validate it. If it is entered, then validate it.

Everything works fine except it is requiring a visit date. How can allow visit date to be null?

I've tried commenting out the html.validationmessagefor and it suppresses the message but still prevents the user from saving the page until a valid date is selected.

From the View (.cshtml):

@* ------ VISIT DATE ------ *@
<div class="col-sm-2">
@Html.LabelFor(model => model.VisitDate, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-sm-2">
    @Html.EditorFor(model => model.VisitDate, new
       {
          htmlAttributes = new
          {
             @Value = Model.VisitDate.ToString("MM/dd/yy"),
             @class = "form-control input-sm"
           },
        })
     @Html.ValidationMessageFor(model => model.VisitDate, "", new { @class = "text-danger" })
</div>

My javascript code:

@section Scripts {

<script type="text/javascript">
  $(function () {
  $('#VisitDate').datetimepicker({
  defaultDate: '',  @*@Model.VisitDate*@
                    format: 'L',
                    showClose: true,
                    showClear: true,
                    toolbarPlacement: 'top',
                    useCurrent: false,
                    date: 'glyphicon glyphicon-calendar'
                });
     });
</script>

@Scripts.Render("~/bundles/jqueryval")

}

Related posts

Recent Viewed