1

I'm working to create a drop down calendar when a input text box is clicked on. I've been trying to model after this (https://www.tutorialspoint.com/jqueryui/jqueryui_datepicker.htm). I've installed jQuery UI Combined using NuGet Package Manager. Though other than the <script> src=... you see there is no other importing to jquery going on. Would somebody please help set me straight on this?

The entire Test.cshtml file

@{
    ViewBag.Title = "Test";
}

<h2>Test</h2>
<div>
<!-- Javascript -->
<script>
     $(function() {
        $( "#datepicker-1" ).datepicker();
     });
</script>

  <!-- HTML -->
  <p>Enter Date: <input type="text" id="datepicker-1"></p>
</div> 

Edit 1: The website is being deployed on an intranet network.

Here is the Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    @using System.Security.Principal
    @using CalcPayment_WebApp.Models.ViewModels
    @using CalcPayment_WebApp.Models.DataBase
    @using System.Linq

    <meta charset="UTF-8">
    <meta name="google" content="notranslate">
    <meta http-equiv="Content-Language" content="en">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>@ViewBag.Title - Some Company</title>
    <link rel="stylesheet" type="text/css" href="~/Content/StyleSheet.css">


    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div 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="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @{
                WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;
                CheckIdentity(identity);
                if (Session["isSysAdmin"] == null || (bool)Session["isSysAdmin"] != true)
                {
                    //show stuff
                }
            }

            @Html.ActionLink("Some Company", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })

        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                @{

                    if (Session["isSysAdmin"] != null && (bool) Session["isSysAdmin"] == true)
                    {
                        <li>@Html.ActionLink("Customers", "Index", "Customers")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    }
                    else
                    {
                        <li>@Html.ActionLink("Some Company", "Details", "Customers", new { id = Session["UserCID"] }, null)</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    }
                }


            </ul>
        </div>
    </div>
</div>
<div class="container body-content">
    @RenderBody()
    <hr />
    <footer>
        <p>&copy; @DateTime.Now.Year - Some Company</p>
    </footer>
</div>

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)


<!--Found at https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sort_table Used for sorting tables-->
<script>
function sortTable(column,targetTable) {
    var table, rows, switching, i, x, y, shouldSwitch;
    table = document.getElementById(targetTable);
    switching = true;
    /*Make a loop that will continue until
    no switching has been done:*/
    while (switching) {
        //start by saying: no switching is done:
        switching = false;
        rows = table.getElementsByTagName("TR");
        /*Loop through all table rows (except the
        first, which contains table headers):*/
        for (i = 1; i < (rows.length - 1); i++) {
            //start by saying there should be no switching:
            shouldSwitch = false;
            /*Get the two elements you want to compare,
            one from current row and one from the next:*/
            x = rows[i].getElementsByTagName("TD")[column];
            y = rows[i + 1].getElementsByTagName("TD")[column];
            //check if the two rows should switch place:
            if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
                //if so, mark as a switch and break the loop:
                shouldSwitch = true;
                break;
            }
        }
        if (shouldSwitch) {
            /*If a switch has been marked, make the switch
            and mark that a switch has been done:*/
            rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
            switching = true;
        }
    }
}
</script>
@functions {
    public void CheckIdentity(WindowsIdentity identity)
    {
        //Check user identity
    }
}

</body>
</html>

Rohlex32
  • 120
  • 9
  • 1
    This code worked fine. Are you using layout page? Can you see any error in console? – derloopkat Aug 11 '17 at 13:29
  • @derloopkat Yes there is a layout page, but I'd rather not share it if it's possible to figure out what's blocking me. No error in console but in order to test my code I'm publishing it each time. 1 console error. SCRIPT438: Object doesn't support property or method 'datepicker' I'm am currently looking into where that leads me. Probably cringy but I'm still a student, sorry I'm learning :/ – Rohlex32 Aug 11 '17 at 13:40
  • 1
    If you can't publish a code that reproduce the error then we can't help. The above code does work. My guess is there's more stuff in your Layout view that might be causing trouble. In your _ViewStart.cshtml try @{ Layout = null; } – derloopkat Aug 11 '17 at 13:44
  • I've made edits to the question – Rohlex32 Aug 11 '17 at 13:49
  • 2
    I see jQuery is being added twice, once in the layout and again in the view. – derloopkat Aug 11 '17 at 13:52
  • 1
    looks like you're probably adding jquery twice (once in your view and one via the jquery MVC bundle). Possibly jqueryUI as well, depending what your bundle contains. Duplicate jQuery instances is always a recipe for problems, and is also completely redundant. – ADyson Aug 11 '17 at 13:54
  • I made additional edits, but I'm blind. Could you please tell me where you see jquery being added in the layout? I'll replace it w/ the jquery from Test.cshtml – Rohlex32 Aug 11 '17 at 13:54
  • 2
    `@Scripts.Render("~/bundles/jquery")`. Check your bundleconfig.cs file to see exactly what is being included in that bundle. – ADyson Aug 11 '17 at 13:55
  • I've found it and seeing if I can follow (https://stackoverflow.com/questions/20081328/how-to-add-jqueryui-library-in-mvc-5-project) to fix my issue. – Rohlex32 Aug 11 '17 at 13:59
  • I've now added jquery-ui to my bundles `bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include( "~/Scripts/jquery-ui-{version}.js")); ` and my layout page now contains `(@Scripts.Render("~/bundles/jqueryui")` but I'm still not having any luck. – Rohlex32 Aug 11 '17 at 14:19

1 Answers1

1

So after viewing (How to add jQueryUI library in MVC 5 project?) I was able to add the jquery ui in the correct spot and was able to get it working. Thanks everyone for the help!

Rohlex32
  • 120
  • 9