weixin_33698823 2013-01-22 17:15 采纳率: 0%
浏览 38

Mvc4 Ajax调用打开新页面

I've looked at what must be 100 posts and can't get this to work in my new project. I've got all the scripts, its seems like the simplist of tests and unobtrusive is set in the webconfig, still no luck getting the actionlink to replace the text in the div. Firebug reports all scripts loaded successfully and code is below. If anyone can help it would be much appreciated and I'd get you a virtual beer!

Controller:

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

View:

@{
ViewBag.Title = "User";
}

<h2>Users</h2>

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

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

Layout:

<!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 of rendered page:

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

Web config:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
  • 写回答

1条回答 默认 最新

  • weixin_33713503 2013-01-22 17:18
    关注

    You should be using Ajax.ActionLink not Html.ActionLink and also you should make sure that MicrosoftAjax.js and MicrosoftMvcAjax.js scripts are loaded in your page if you are using MVC 2 or older (your rendered html does not show them).

    You're looking for an output like this:

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

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)