简体   繁体   中英

datepicker bootstrap issue, always set the attribute input-validation-error, I don´t want required

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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