Jquery对话框在MVC中不起作用

时间:2012-11-05 02:18:20

标签: jquery asp.net-mvc jquery-ui jquery-ui-dialog

现在我知道它之前被问过,但我似乎无法让JQUERY对话框起作用。

我有一个简单的例子,我从JQUERY网站获得,但对话框永远不会弹出。

我的_Layout.cshtml页面

    <!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>

我的Index.cshtml页面

    <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>

警告弹出,但对话框没有,任何想法为什么?

编辑:做了一些编辑,现在我有了这个。

<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>

现在,当我有三个警报时,它会提醒HELLO和HELLO2,但它永远不会HELLO3 ..但是如果我将HELLO3脚本放在对话框脚本之上,它将显示所有3个警报。有什么想法发生了什么?

2 个答案:

答案 0 :(得分:1)

它只能自行查找:http://jsfiddle.net/Dy2Xw/

检查脚本以确保它们正确加载。此外,您似乎正在加载jquery base js两次。

<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>

您还有两个css文件 - 不确定为什么需要这两个文件:

<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" />

清理那些脚本加载,检查控制台是否存在任何js错误,只输入您需要的内容并再次尝试。

答案 1 :(得分:0)

可能与</body> vs <head>标记之前的js文件位置有关。将@Scripts.Render("~/bundles/jquery")移至<head></head>代码