不引人注意的AJAX错误:“未捕获的ReferenceError:未定义系统”

时间:2022-11-06 16:27:43

Example code: In an MVC 5 project in Visual Studio, I have set up the following controller and views:

示例代码:在Visual Studio的MVC 5项目中,我设置了以下控制器和视图:

Controller

调节器

TestController.cs

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

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

PartialView1.cshtml

<h1>Test 1</h1>

PartialView2.cshtml

PartialView2.cshtml

<h1>Test 2</h1>

Question

If I have understood correctly, clicking "Partial 1" should load the contents of "PartialView1" into the HTML element with an ID of "contentShouldLoadHere". Instead, clicking "Partial 1" causes a normal page load of the MyPartialView1 action (~/Test/MyPartialView1), and in the Chrome dev tools console I get the following JavaScript error:

如果我已正确理解,单击“Partial 1”应将“PartialView1”的内容加载到ID为“contentShouldLoadHere”的HTML元素中。相反,单击“部分1”会导致MyPartialView1操作(〜/ Test / MyPartialView1)的正常页面加载,并且在Chrome开发工具控制台中,我收到以下JavaScript错误:

Uncaught ReferenceError: Sys is undefined

Does anyone know why this is, and how I can fix it?

有谁知道这是为什么,以及我如何解决它?

Notes - I have confirmed via Chrome Dev Tools that both scripts are being correctly loaded. - The HTML for the link generated by the @Ajax helper is:

注意 - 我已通过Chrome开发工具确认两个脚本都已正确加载。 - @Ajax助手生成的链接的HTML是:

<a href="/Test/MyPartialView1" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: &#39;contentShouldLoadHere&#39; });">Partial 1</a>

1 个解决方案

#1


23  

The html your generating for the link is generated if unobtrusive javascript has been disabled. You can enable it in the view with

如果禁用了不显眼的javascript,则会生成为链接生成的html。您可以在视图中启用它

@{HtmlHelper.UnobtrusiveJavaScriptEnabled = true;}

or for the application (in web.config) with

或者用于应用程序(在web.config中)

<configuration>
  <appSettings>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>

Your rendered html should then look like

你渲染的html应该是这样的

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#contentShouldLoadHere" href="/Test/Partial1">Partial 1</a>

and will work with the jquery.unobtrusive-ajax.js file

并将使用jquery.unobtrusive-ajax.js文件

#1


23  

The html your generating for the link is generated if unobtrusive javascript has been disabled. You can enable it in the view with

如果禁用了不显眼的javascript,则会生成为链接生成的html。您可以在视图中启用它

@{HtmlHelper.UnobtrusiveJavaScriptEnabled = true;}

or for the application (in web.config) with

或者用于应用程序(在web.config中)

<configuration>
  <appSettings>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>

Your rendered html should then look like

你渲染的html应该是这样的

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#contentShouldLoadHere" href="/Test/Partial1">Partial 1</a>

and will work with the jquery.unobtrusive-ajax.js file

并将使用jquery.unobtrusive-ajax.js文件