-1

I have created a very simple DatePicker using .NET Core. Here is my model:

public class MemberViewModel
    {
        public string Name { get; set; }
        [DisplayFormat(DataFormatString = @"{0:dd\/MM\/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime DOB { get; set; }
    }

Here is my View:

@model DatePicker.Models.MemberViewModel

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

    <h2>MemberView</h2>

    <h4>MemberViewModel</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="MemberView">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <input asp-for="Name" class="form-control" />
                    <span asp-validation-for="Name" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="DOB" class="control-label"></label>
                    @Html.TextBox("datepicker")
                    @*<input asp-for="DOB" class="form-control" />
                        <span asp-validation-for="DOB" 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");}
    }

Here is my Layout:

<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery UI Datepicker - Restrict date range</title>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>
        $(function () {
            $("#datepicker").datepicker({ minDate: -20, maxDate: "+1M +10D", dateFormat: "dd/mm/yy" });
        });
    </script>

The DatePicker does nothing when I click on the textbox (in all browsers). The strange thing is; if I copy and paste the HTML generated into a HTML file and then double click on the HTML file, then the DatePicker works as expected. What is the problem?

Update

Please see the HTML generated below:

<!DOCTYPE html>
<html>
<head>



    <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery UI Datepicker - Restrict date range</title>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>
        $(function () {
            $("#datepicker").datepicker({ minDate: -20, maxDate: "+1M +10D", dateFormat: "dd/mm/yy" });
        });
    </script>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>MemberView - DatePicker Hotla</title>

</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="/">DatePicker</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a href="/">Home</a></li>
                    <li><a href="/Home/About">About</a></li>
                    <li><a href="/Home/Contact">Contact</a></li>
                </ul>
            </div>
        </div>
    </nav>





    <div class="container body-content">


<h2>MemberView</h2>

<h4>MemberViewModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form action="/Home/MemberView" method="post">

            <div class="form-group">
                <label class="control-label" for="Name">Name</label>
                <input class="form-control" type="text" id="Name" name="Name" value="" />
                <span class="text-danger field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
            </div>
            <div class="form-group">
                <label class="control-label" for="DOB">DOB1</label>
                <input id="datepicker" name="datepicker" type="text" value="" />
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        <input name="__RequestVerificationToken" type="hidden" value="CfDJ8AZxPMp7tE9EgVGFaTl7fvF-ALxvEVCrHkYj_isQTt5HUG2iu3EPMhNjR_6OlI2xtybr9grj4e4c3eFwe5jY-8r5YWeWIdtSNkc5IsvYnt9JYc5ftpPSzAqzINzih1yg85DFHJLEkWRZ2EjvupuQQ5E" /></form>
    </div>
</div>

<div>
    <a href="/">Back to List</a>
</div>


        <hr />
        <footer>
            <p>&copy; 2018 - DatePicker</p>
        </footer>
    </div>


        <script src="/lib/jquery/dist/jquery.js"></script>
        <script src="/lib/bootstrap/dist/js/bootstrap.js"></script>
        <script src="/js/site.js?v=4q1jwFhaPaZgr8WAUSrux6hAuh0XDg9kPS3xIVq36I0"></script>

    <script src="/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

</body>
</html>

If I comment out the following line, then it works as expected:

<script src="/lib/jquery/dist/jquery.js"></script>
w0051977
  • 15,099
  • 32
  • 152
  • 329
  • Show the HTML that arrives in the browser. You servercode is irrelevant. – connexo Oct 21 '18 at 10:54
  • Using an id as selector means you only have one datepicker in the whole page. Correct? – connexo Oct 21 '18 at 11:06
  • @connexo, I have updated the question. I understand the problem now, however I do not understand the solution. – w0051977 Oct 21 '18 at 11:22
  • Do I have to install the jQuery UI Combined library. I have tried this, however I cannot see any new JavaScript files. – w0051977 Oct 21 '18 at 11:23
  • You are including jQuery twice. The second jQuery inclusion overwrites your `$` variable, removing the jQuery ui plugins you loaded beforehand. – connexo Oct 21 '18 at 11:24
  • @connexo, I realise that. How can I generate the datepicker using: lib/jquery/dist/jquery.js ? Thanks. – w0051977 Oct 21 '18 at 11:25
  • You are including jQuery twice but also notice the order you are including othet JS files. All other JS files should be below the first jQuery file. – SuperDJ Oct 21 '18 at 11:25
  • Replace `` with ``. – connexo Oct 21 '18 at 11:27
  • And you **must** stop trying to do **anything** via the `file://` protocol. – connexo Oct 21 '18 at 11:28
  • @connexon, I don't think /lib/jquery/dist/jquery.js contains the Datepicker. Changing to makes no difference. – w0051977 Oct 21 '18 at 11:32
  • The datepicker comes with `` which - as every jQuery plugin - must be loaded *after* jQuery. It seems you're refusing to learn the most basic stuff about what your site is using. – connexo Oct 21 '18 at 12:45

1 Answers1

4

You are including jQuery twice. The second jQuery inclusion overwrites your $ variable, removing the jQuery ui plugins you loaded beforehand.

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

and

<script src="/lib/jquery/dist/jquery.js"></script>

jQuery plugins work by extending the $ variable. You second inclusion of jQuery destroys the plugins previously loaded.

connexo
  • 53,704
  • 14
  • 91
  • 128
  • If I comment out the first (), then it does not work. How can I get it to work with the second reference ()? – w0051977 Oct 21 '18 at 11:28
  • 1
    You need to load jQuery prior to any other Javascripts. – connexo Oct 21 '18 at 11:28
  • @w0051977, if this fixed the problem please mark this answer as solution. – derloopkat Oct 21 '18 at 11:33
  • Before I accept - do you know if there is a calendar theme that allows you to browse by year. The calendar theme I have only appears to allow you to browse by month. If I want to go back to 1940 then it takes about five minutes. Thanks. +1. – w0051977 Oct 22 '18 at 08:02
  • That would be a new question. – connexo Oct 22 '18 at 08:32