0

I'm trying to load the scripts for my application. Do not work so I checked a simple alert ('test11');. It also does not work, the script is not loaded at all? It uses standard _Layout of MVC4 generated by Visual Studio 2013.

View Details.cshtml

@model xzy.Models.Album

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

@using (Html.BeginForm("Details", "Store", FormMethod.Post,
    new
    {
        id = "someID",
        data_oneAction = @Url.Action("one"),
        data_twoAction = @Url.Action("two")
    }))
{
    <fieldset

        <label for="Formats">Select format</label>

        @Html.DropDownList("Formats", ViewBag.Formats as SelectList,
  new { id = "FormatsID", @class = "xxx" })


    <input type="submit" value="Submit" id="SubmitID" } />

    </fieldset>
}

<script src="@Url.Content("~/Scripts/first_file.js")"></script>
<script src="@Url.Content("~/Scripts/second_file.js")"></script>

Script first_file.js

$(function () {

    $('#FormatsID').change(function () {
        alert('Test11');
    });
});

UPDATE

Yaakov Ellis, it works. But when I have a script with the update, then it not works.

$(function () {

    $('#FormatsID').change(function () {
        alert('Test11');
        var URL = $('#someID').data('oneAction');
        $.getJSON(URL + '/' + $('#FormatsID').val(), function (data) {
            alert('Test22');
        });
    });
});
3
  • 1
    If you look at the source code for the page, does it show the script being loaded? Commented Jun 22, 2014 at 10:50
  • shows that it has been loaded. But it not works. When I add it @Scripts.Render("~/bundles/jquery") before the <script src ... then it works. Commented Jun 22, 2014 at 10:57
  • So now you are asking a completely different question - why a specific piece of jQuery doesn't work. Perhaps this should be asked in a new question, since the original issue in this one has been answered. And when asking, it might be helpful to include the html that is produced by your page, what is included in #FormatsID, what errors if any come up in the console logs, what you get when you set javascript breakpoints and step through the code, etc. Commented Jun 22, 2014 at 11:35

1 Answer 1

2

The content of first_file.js is dependent on the presence of jQuery. I suspect that if you check your console log, you will see errors on the first line of this file.

This is confirmed by the fact that when you add in a jQuery reference prior to this file (as you wrote in your comment), it works.

So if you want to use this script as-is, be sure to include jQuery first.

If you just want a better test of the first_file.js file, then remove all of the jQuery stuff from it. Its sole contents should be one line with alert('Test11'); on it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.