(提前为长篇道歉,但在我的智慧结束)..
在我的 MVC 3 应用程序中,我遇到了一个奇怪的问题,即 jquery 脚本“丢失”,因为需要一个更好的词。
在我的应用程序中,我有以下 _Layout.cshtml 文件:
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("../Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("../Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>
<body>
<div class="page">
<div id = "sidebar">
@{ Html.RenderPartial("_Sidebar"); }
</div>
<div id="main">
@RenderBody()
</div>
<div id="validationresults">
<div id="validationresultsinner"></div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
侧边栏本身是一个局部视图,其中包含几个 Ajax.ActionLink,它们将父视图加载到我的 _Layout.cshtml 的“主”区域中。
<ul>
<li>@Ajax.ActionLink("Section 1", "WizardParentView", "Section1",null, new AjaxOptions()
{ UpdateTargetId = "main", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }, new { id = "sect2" })</li>
<li>@Ajax.ActionLink("Section 2", "WizardParentView", "Section2", null, new AjaxOptions()
{ UpdateTargetId = "main", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }, new { id = "sect2" })</li>
</ul>
此 WizardParentView 本身包含一个 Ajax.Begin 表单,该表单与 NuGet 的 MVCWizard.Wizard 一起使用,以托管用户可以浏览的页面选择(例如向导类型页面)
例如
@model MyDataModel
@{
FormWizardManager<MyDataModel> wm = (FormWizardManager<MyDataModel>)Session["TheSection"];
Layout = "";
}
@using (Ajax.BeginForm("WizardParentView", "Section1", new AjaxOptions() {
UpdateTargetId= "wizard", InsertionMode = InsertionMode.Replace }))
{
<div id="wizard">
@Html.WizardHeader(wm, true)
@{Html.RenderAction(wm.CurrentPage.PagePartialView);}
@Html.WizardFooter(wm, "Next Page " + (wm.Index + 1), "Previous Page" + (wm.Index - 1), "Save this Section")
</div>
}
@Content.Script("jquery.validate.js", Url)
@Content.Script("jquery.validate.unobtrusive.min.js", Url)
@Content.Script("UIHelpers.Section1.js", Url)
我还具有一些必需的功能来修改侧栏中的项目文本,以永久反映用户通过向导的进度状态。此文本修改是通过在其他地方执行回发(调用服务器端验证时)的另一位 JQuery 来实现的。如您所见,为了在再次单击侧边栏链接时(在它可能已被修改之后)停止原始侧边栏部分重新加载,我还必须在 WizardParentView 加载时清除布局。
问题是,当我在开发环境(带有 IIS7 Express 或 Visual Studio 2010 Web 开发服务器的 IE9)中运行它时,所有需要 JQuery 或 JQuery.Unobtrusive.Ajax 的页面加载和功能都可以正常工作
但是,在完整的 II7 或 II6 下运行相同的网站(见下文)我发现核心脚本和所有相关功能在前 2 个页面导航尝试中加载和工作正常,但在第三个页面上(例如第三次按下“下一步”) ,JQuery 脚本肯定不再存在。这意味着我的部分视图加载到完整的网页中,并且视图中的任何 jquery 功能都停止工作。
进一步的混乱如下:
工作 JQuery 1.5.1 IE9、IIS7 Express、Visual Studio Web 开发服务器
损坏
的 JQuery 1.5.1 IE9、IIS6、IIS7、IIS7 Express
JQuery 1.5.1 Chrome/Firefox、IIS6、IIS7、II7 Express、Visual Studio Web 开发服务器
JQuery 1.8.1 IE9、IIS6、IIS7、IIS7 Express、Visual Studio Web 开发服务器
我认为即使在第一次加载 WizardParentView 时清除布局也会清除我在 _Layout 中加载的 JQuery 脚本,但是基于 JQuery 的功能在 2-3 次导航尝试后仍然有效然后死掉的事实很奇怪。 . 更不用说应用程序功能至少在上述配置之一中完美运行(JQuery 脚本不应该在所有情况下都被清除吗?或者某种特定的浏览器组合会发生某种奇怪的缓存, JQuery 版本和 Web 服务器?.. 抱歉,我有点不知所措,无法更好地描述它.. 并为令人困惑的解释道歉(在我自己的脑海中,我可以清楚地描述这个问题):)