2

I'm trying to call bootstrap-datepicker, from looking at similar issues on here I think I'm referencing it correctly but when I open chrome developer, jQuery is not defined, screenshot of developer in image link below

BundlesConfig

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                    "~/Scripts/bootstrap.js",
                    "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                    "~/Content/bootstrap.css",
                    "~/Content/site.css"));

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery.unobtrusive-ajax.min.js"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap-datetimepicker").Include(
                        "~/Scripts/moment.js",
                        "~/Scripts/bootstrap-datetimepicker.js"));

        bundles.Add(new StyleBundle("~/Content/bootstrap-datetimeicker").Include(
                        "~/Content/bootstrap-datetimepicker.min.css"));
    }

_Layout page

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Public Events</title>
    @Styles.Render("~/Content/css")
    @RenderSection("styles", required: false)
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</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>
            @Html.ActionLink("Events", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                @if (Request.IsAuthenticated)
                {
                    <li>@Html.ActionLink("My Events", "My", "Events")</li>
                    <li>@Html.ActionLink("Create Event", "Create", "Events")</li>
                }
            </ul>
            @Html.Partial("_LoginPartial")
        </div>
    </div>
</div>
<div class="container body-content">
    @Html.Partial("_Notifications")
    @RenderBody()
    <hr />
    <footer>
        <p>&copy; @DateTime.Now.Year - Events Application</p>
    </footer>
</div>

Create page

@model Events.Web.Models.EventInputModel

@{
    ViewBag.Title = "Create New Event";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Create</title>

</head>
<body>
<h2>Create</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>EventInputModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.StartDateTime, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StartDateTime, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.StartDateTime, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Duration, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Duration, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Duration, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IsPublic, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.CheckBoxFor(model => model.IsPublic, htmlAttributes: new { @checked = "true" })
                    @Html.ValidationMessageFor(model => model.IsPublic, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
                @Html.ActionLink("Cancel", "My", null, htmlAttributes: new { @class = "btn" });
            </div>
        </div>
    </div>
}
@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap-datetimepicker")

    <script>
        $(function () {
            $('#StartDateTime').datetimepicker(
            {
                format: 'DD-MMM-YYYY HH:mm',
                sideBySide: true,
                showTodayButton: true
            });
        });
    </script>
}
@section styles{
    @Styles.Render("~/Content/bootstrap-datetimepicker")
}

Chrome Developer

Scripts folder

Michael O'R
  • 31
  • 1
  • 7
  • This line: `@RenderSection("styles", required: false)` should be below this line: 1@Scripts.Render("~/bundles/jquery")1. Like this: `@Scripts.Render("~/bundles/jquery")` `@RenderSection("styles", required: false)` – Marcin Jan 25 '16 at 14:25
  • @Marcin - why would it matter that jquery is loaded before ***styles***? – Jamiec Jan 25 '16 at 14:26
  • @Jamiec shi.. I saw `RenderSection("scripts"...` my foult sry :/ – Marcin Jan 25 '16 at 14:28
  • Are you using the `_Layout` page? You don't have a `@RenderSection("scripts", required: false)` in the layout you have listed. – krillgar Jan 25 '16 at 14:34
  • @krillgar Sorry for not copying it in the question code, I actually had that RenderSection in _Layout, but had it at the end of the body, I have moved it to the head as per Jamiec's solution but no change – Michael O'R Jan 25 '16 at 14:49
  • 1
    Then either you don't have a jQuery file on the server (I would think that would throw an exception when creating the bundle), or that Create page is using a different layout page. Those would be my first two places to check. – krillgar Jan 25 '16 at 15:16
  • @krillgar The _Layout page is definitely being used as the nav bar and log in are coming from there, I was thinking it doesn't seem to be but I've uninstalled and reinstalled jQuery-2.2.0 and tried downgrading to 2.1.4 to no avail – Michael O'R Jan 25 '16 at 15:28
  • Try adding jQuery to the modernizr bundle at the top. See if that helps. Maybe you have a typo? – krillgar Jan 25 '16 at 15:38
  • You have defined 2 `"~/bundles/jquery"` bundles (one that include `jquery-{version}.js` and the other that includes `jquery.unobtrusive-ajax.min.js`. –  Jan 27 '16 at 02:36

1 Answers1

1

You'll see from your screenshot that jquery is not loaded as one of the scripts, which invariably means you have forgotten to include the jquery bundle on that page.

@section scripts{
    @Scripts.Render("~/bundles/jquery") // <-- this'll probably fix it
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap-datetimepicker")

Alternatively, you may have just forgotten to include the section in Layout

@Styles.Render("~/Content/css")
@RenderSection("styles", required: false)
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/modernizr")
@RenderSection("scripts", required: false) // <-- placed *after* the jquery bundle

In that scenario, you should NOT also include it in the scripts section directly.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 1
    Should it not be loaded by the scripts section on the _Layout page? - http://stackoverflow.com/questions/17733509/is-not-defined-asp-net-mvc-4 Also to avoid having referenced jQuery twice (as in your case) double check if you haven't included it already as a bundle in your _Layout. – Michael O'R Jan 25 '16 at 14:27
  • 1
    @MichaelO'R - well, that is the confusing bit. Your layout page seems to include a section for "styles" but then your create page defines a section called "scripts". You're either missing the section definition for scripts in Layout, or missing the bundle include in create... it's not clear which (hence: "this'll *probably* fix it....") – Jamiec Jan 25 '16 at 14:29
  • Sorry for not copying it in the question code, I actually had that RenderSection in _Layout, but at the end of the body, I have moved it to the head as per your solution but no change – Michael O'R Jan 25 '16 at 14:43
  • And you're ***sure*** that you're actually using `_Layout`? – Jamiec Jan 25 '16 at 15:20
  • Positive, the nav bar and login are coming from there – Michael O'R Jan 25 '16 at 15:29
  • Ok, the next thing to do then is view source on your `Create` page - each `@Scripts.Render....` line should result in one or more ` – Jamiec Jan 25 '16 at 15:35
  • Ok, so jQuery definitely is not being included, no script src is being generated – Michael O'R Jan 25 '16 at 15:41
  • Is `modernizr`? What's the line directly above it in HTML? I think you need to show us the entire `Layout` and `Create` pages – Jamiec Jan 25 '16 at 15:42
  • Modernizer was being included, changed the order, jquery after modernizer but no change, full pages up as requested – Michael O'R Jan 25 '16 at 18:56