Mvc4 Ajax Call打开新页面

时间:2013-01-22 17:15:11

标签: ajax jquery asp.net-mvc-4

我已经查看了必须包含的100个帖子,并且无法在我的新项目中使用它。我有所有的脚本,它似乎是简单的测试和unobtrusive在webconfig中设置,仍然没有运气获取actionlink替换div中的文本。 Firebug报告所有脚本已成功加载,代码如下。如果有人可以提供帮助,我将非常感激,我会给你一个虚拟啤酒!

控制器:

public ActionResult AjaxTest()
{
return Content("Ajax Test Successful");
}

查看:

@{
ViewBag.Title = "User";
}

<h2>Users</h2>

@Html.ActionLink("Ajax Test", "AjaxTest", "Admin", new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "ajaxtest" })

<div id="ajaxtest"></div>

布局:

<!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" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">
<img src="~/Images/logo.jpg" /></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("Upload Files", "UploadFiles", "Upload")</li>
<li>@Html.ActionLink("Upload History", "UploadHistory", "Upload")</li>
<li>@Html.ActionLink("Help", "Help", "Home")</li>
<li>@Html.ActionLink("Admin", "Home", "Admin")</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")
@Scripts.Render("~/bundles/jqueryval")

@RenderSection("scripts", required: false)
</body>
</html>

呈现网页的HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>User - My ASP.NET MVC Application</title>
    <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    <link href="/Content/site.css" rel="stylesheet"/>
<link href="/Content/buttons.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.5.3.js"></script>

</head>
<body>
    <header>
        <div class="content-wrapper">
            <div class="float-left">
                <p class="site-title">
                    <img src="/Images/logo.jpg" /></p>
            </div>
            <div class="float-right">
                <section id="login">
                        <ul>
    <li><a href="/Account/Register" id="registerLink">Register</a></li>
    <li><a href="/Account/Login" id="loginLink">Log in</a></li>
</ul>

                </section>
                <nav>
                    <ul id="menu">
                        <li><a href="/">Home</a></li>
                        <li><a href="/Upload/UploadFiles">Upload Files</a></li>
                        <li><a href="/Upload/UploadHistory">Upload History</a></li>
                        <li><a href="/Home/Help">Help</a></li>
                        <li><a href="/Admin/Home">Admin</a></li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
    <div id="body">

        <section class="content-wrapper main-content clear-fix">


<h2>Users</h2>

<a Confirm="" HttpMethod="" InsertionMode="Replace" LoadingElementDuration="0" LoadingElementId="" OnBegin="" OnComplete="" OnFailure="" OnSuccess="" UpdateTargetId="ajaxtest" Url="" href="/Admin/AjaxTest?Length=5">Ajax Test</a>

<div id="ajaxtest"></div>
        </section>
    </div>
    <footer>
        <div class="content-wrapper">
            <div class="float-left">
                <p>&copy; 2013 - My ASP.NET MVC Application</p>
            </div>
        </div>
    </footer>

    <script src="/Scripts/jquery-1.7.1.js"></script>

    <script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>



</body>
</html>

网络配置:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

1 个答案:

答案 0 :(得分:1)

您应该使用Ajax.ActionLink而不是Html.ActionLink,如果您使用的是MVC 2或更早的版本,您应确保在您的页面中加载了MicrosoftAjax.js和MicrosoftMvcAjax.js脚本(您呈现的HTML)不显示它们。)

您正在寻找这样的输出:

<a data-ajax="true" data-ajax-method="Get" data-ajax-mode="replace" data-ajax-update="#ajaxtest" href="/Home/About">Ajax Test</a>