示例代码: 在Visual Studio的MVC 5项目中,我已经设置了以下控制器和视图:
控制器
TestController.cs
public class TestController : BaseController
{
public ActionResult Index()
{
return View("Index");
}
public PartialViewResult MyPartialView1()
{
return PartialView("PartialView1");
}
public PartialViewResult MyPartialView2()
{
return PartialView("PartialView2");
}
}
Views
Index.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<script src="@Url.Content("~/Scripts/jquery-2.1.1.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
</head>
<body>
<h1>Testing Unobtrusive AJAX</h1>
@Ajax.ActionLink("Partial 1", "MyPartialView1", "Test",
new AjaxOptions() { UpdateTargetId = "contentShouldLoadHere", InsertionMode = InsertionMode.Replace })
|
@Ajax.ActionLink("Partial 2", "MyPartialView2", "Test",
new AjaxOptions() { UpdateTargetId = "contentShouldLoadHere", InsertionMode = InsertionMode.Replace })
<div id="contentShouldLoadHere">
<p>This will get replaced.</p>
</div>
</body>
</html>
PartialView1.cshtml
<h1>Test 1</h1>
PartialView2.cshtml
<h1>Test 2</h1>
问题
如果我理解正确,点击"Partial 1"应该将“PartialView1”的内容加载到具有ID“contentShouldLoadHere”的HTML元素中。但是,点击"Partial 1"会导致MyPartialView1动作(〜/Test/MyPartialView1)
的普通页面加载,并且在Chrome开发者工具控制台中,我得到以下JavaScript错误:
Uncaught ReferenceError: Sys is undefined
有人知道为什么会这样,我应该如何修复?
注: - 我已通过Chrome Dev Tools确认两个脚本都正确加载。 - 由@Ajax助手生成的链接HTML为:
<a href="/Test/MyPartialView1" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'contentShouldLoadHere' });">Partial 1</a>
<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#contentShouldLoadHere" href="/Test/Partial1">部分1</a>
,所以这里有些奇怪。 - user3559349UnobtrusiveJavaScriptEnabled=false
? - user3559349<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
这一行。我添加了它,问题得到了解决。请随意将其作为答案添加,我会接受它。 - Nick Coad