Claud
Claud

Reputation: 1075

Jquery dialog isn't working in MVC

Now I know it's been asked before but I can't seem to get JQUERY dialog to work.

I have an easy example that I got from the JQUERY website but the dialog never pops up.

My _Layout.cshtml page

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>@ViewBag.Title - My ASP.NET MVC Application</title>
            <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
            <meta name="viewport" content="width=device-width" />
            <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
            <script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
            @Styles.Render("~/Content/css")
            @Scripts.Render("~/bundles/modernizr")
        </head>
        <body>
            <header>
                <div class="content-wrapper">
                    <div class="float-left">
                        <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                    </div>
                    <div class="float-right">
                        <section id="login">
                            @Html.Partial("_LoginPartial")
                        </section>
                        <nav>
                            <ul id="menu">
                                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                                <li>@Html.ActionLink("About", "About", "Home")</li>
                                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <div id="body">
                @RenderSection("featured", required: false)
                <section class="content-wrapper main-content clear-fix">
                    @RenderBody()
                </section>
            </div>
            <footer>
                <div class="content-wrapper">
                    <div class="float-left">
                        <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                    </div>
                </div>
            </footer>

            @Scripts.Render("~/bundles/jquery")
            @RenderSection("scripts", required: false)
        </body>
    </html>

My Index.cshtml page

    <div id="dialog" title="Basic dialog">
        <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
    </div>

<script type="text/javascript">
    $(function () {
        alert("HELLO");
    });
</script>

    <script type="text/javascript">
        $(function () {
            $("#dialog").dialog();
        });
    </script>

The alert pops up but the dialog doesn't, any idea why?

EDIT: Did some editing and now I have this.

<div id="dialog" title="Basic dialog">
    <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

<script type="text/javascript">
    $(function () {
        alert("HELLO");
    });
</script>

<script type="text/javascript">
    $(function () {
        alert("HELLO2");
    });
</script>

<script type="text/javascript">
    $(function () {
        $("#dialog").dialog();
    });
</script>

<script type="text/javascript">
    $(function () {
        alert("HELLO3");
    });
</script>

Now when I have three alerts, it will alert HELLO and HELLO2 but it never does HELLO3.. however if I put the HELLO3 script above the dialog script it will display all 3 alerts. Any ideas what is going on?

Upvotes: 2

Views: 2825

Answers (2)

parsh
parsh

Reputation: 756

Maybe it has to do with location of js files before the </body> vs <head> tag. Move @Scripts.Render("~/bundles/jquery") to <head></head> tag

Upvotes: 0

jk.
jk.

Reputation: 14435

It works just find by itself: http://jsfiddle.net/Dy2Xw/

Check your scripts to make sure they are loading properly. Also, it appears you are loading the jquery base js twice.

<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>

<script src="http://code.jquery.com/jquery-1.8.2.js "></script>

You also have two css files - not sure why you would need both of those:

<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/themes/base/jquery.ui.dialog.css")" rel="stylesheet" type="text/css" />

Clean up those script loads, check the console for any js errors, put in only what you need and try it again.

Upvotes: 1

Related Questions