Search code examples
asp.net-core-mvcjquery-ui-datepicker

Trouble with JQuery DatePicker in .NetCore 2.0 MVC


I seem to be struggling to get the Jquery datepicker working in my basic CRUD application. I have used NuGet Package Manger to install Jquery UI.Combined library:

https://ibb.co/mmEfic

In my _Layout.cshtml file I have the following script tags:

    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        $(function () {
            $("#datepicker").datepicker();
        });
    </script>

(The _Layout.cshtml is referenced in my ViewStart file)

Finally, I am trying to use the Datepicker in my View:

@model GymTracker.ViewModel.MemberViewModel

@{
    ViewData["Title"] = "Create";
}

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">



<h2>Create</h2>

<h4>Member</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DateOfBirth" class="control-label"></label>

           @Html.TextBox("datetimepicker")


            </div>
            <div class="form-group">
                <label asp-for="PaymentType" class="control-label"></label>
                @Html.DropDownListFor(model => model.PaymentType, Html.GetEnumSelectList(typeof(Membershiptype)))

                <span asp-validation-for="PaymentType" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>




@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Can anyone see what I am doing wrong. Surely the line :

@Html.TextBox("datetimepicker")

Would render the Jquery date picker?

Any help would be appreciated. Thank you


Solution

  • I had the same problem but never found a "clean" solution - but this workaround does the job for me:

    Put up a simple div and input field and then hide the actual form field (adjust alignment with spaces):

    <div>
                &nbsp;  &nbsp; Ride Date <br />
                &nbsp;  &nbsp; <input type="text" id="datepicker">
            </div>
    
            <div class="form-group d-none">
                <label asp-for="RideDate" class="col-md-6 control-label"></label>
                <div class="col-md-6">
                    <input asp-for="RideDate" class="form-control" />
                    <span asp-validation-for="RideDate" class="text-danger"></span>
                </div>
                <br />
            </div>

    Put an ID on the Submit:

            <div class="row justify-content-md-center">
                <div class="form-group">
                    <div class="col-md-5">
                        <button id="fixtime" type="submit" class="btn btn-primary">Add Comment</button>
                    </div>
                </div>
            </div>

    Then move user selected date to actual input field:

        <script>
            $(document).ready(function () {
                $('#datepicker').datepicker({
                    uiLibrary: 'bootstrap4'
                });
                //load current date into datapicker
    
                var now = new Date();
                var day = ("0" + now.getDate()).slice(-2);
                var month = ("0" + (now.getMonth() + 1)).slice(-2);
                var today = month + '/' + day + '/' + now.getFullYear();
                //alert(today);
                $('#datepicker').val(today);
    
                //$("#RideDate").val(now.getFullYear() + "-" + month + "-" + day);  // not needed
    
                $("#fixtime").click(function () {
                    var fix = $("#datepicker").val(); // from datepicker mm/dd/yyyy to ASP yyyy-mm-dd
                    var fix2 = fix.substr(6) + "-" + fix.substr(0, 2) + "-" + fix.substr(3, 2);
                    $("#RideDate").val(fix2);
                    //alert("ride date = " +  $("#RideDate").val());
                });
            });
        </script>

    Not very elegant but it works OK. m