0

I have an issue with datepicker bootstrap, in aspnet core project.

I use datepicker bootstrap v.1.6.4, and the problem is, the datepicker is always required, but I don´t want it required.

My ViewModel .cs

namespace VOICE.Users
{
    public class UserViewModel
    {
        public string Id { get; set; }
        public DateTime DateCreation { get; set; }
        ....//other properties
    }
}

My View .cshtml

@model VOICE.Users.UserViewModel
.....//html tags

        <div class="form-group" id="data_1">
            <label>Date</label>
            <div class="input-group date">
                <span class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                </span>
                <input asp-for="DateCreation" class="form-control">
            </div>
        </div>

    ....


@section Styles {
    <link href="~/lib/bootstrap-datepicker/dist/css/bootstrap-datepicker3.css" rel="stylesheet" />
}


@section Scripts {
    <script src="/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
    <script src="~/lib/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js"></script>
    <script src="~/lib/bootstrap-datepicker/dist/locales/bootstrap-datepicker.pt.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {

            $('#data_1 .input-group.date').datepicker({
                language: "pt",
                todayHighlight: true,
                todayBtn: "linked",
                keyboardNavigation: false,
                forceParse: false,
                calendarWeeks: true,
                autoclose: true
            });
        });
    </script>
    }

when the page loads I have this in the web page

<input class="form-control" type="datetime" data-val="true" data-val-required="The DateCreation field is required." id="DateCreation" name="DateCreation" value="">

When I click the submit button the datepicker is now in mode required and I have this

<input class="form-control input-validation-error" type="datetime" data-val="true" data-val-required="The DateCreation field is required." id="DateCreation" name="DateCreation" value="" aria-required="true" aria-describedby="DateCreation-error" aria-invalid="true">

How can I prevent the datapicker to be required? I try to remove the attribute data-val-required but no luck, the problem is the input-validation-error added to the class.

Best regards.

Jolynice

Suresh Karia
  • 17,550
  • 18
  • 67
  • 85
jolynice
  • 514
  • 1
  • 8
  • 25

1 Answers1

0

Based upon an answer here (go give them the up votes if this is correct). The DateTime needs to be explicitly set as nullable due to it being a value type. So the following should work:

 public Nullable<DateTime> DateCreation { get; set; }

or

 public DateTime? DateCreation { get; set; }
Community
  • 1
  • 1
  • Many thanks AsteriskTheServer. Yes that´s , now is working fine, but this behavior is very strange. – jolynice Apr 08 '17 at 21:55