我想<div>在用户点击时更新我的视图Ajax.ActionLink.但是,它始终导航整个页面,而不是将服务器的响应插入到我指定的元素中.
我觉得我在这个例子中正在做所有事情,但即使在创建最简单的测试用例之后,我仍然无法创建我想要的行为.
在接下来的测试用例中,我加载/Company/test并点击"Go!"后,我希望将<div>其替换为"All done",而是将整个页面导航到/Company/test_ajax.
我确定我在这里遗漏了一些东西.提前致谢.
CompanyController
public ActionResult test()
{
return View();
}
public ActionResult test_ajax()
{
return Content("All done");
}
Run Code Online (Sandbox Code Playgroud)
Test.aspx文件
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
</head>
<body>
<h2>test</h2>
<%= Ajax.ActionLink("Go!", "test_ajax",
new AjaxOptions {
UpdateTargetId="viewport"
}) %>
<div id="viewport">Replace me!</div>
</body></html>
Run Code Online (Sandbox Code Playgroud)
您的示例在我的计算机上按预期运行。检查文件 MicrosoftAjax.js 和 MicrosoftMvcAjax.js 是否确实存在于 ../../Scripts 文件夹中。