MVC2:当Details页面被分页时,JQuery无法正常工作

时间:2010-12-13 20:42:04

标签: jquery asp.net-mvc-2 pagination master-detail

问题:

我有一个使用Details页面和Master页面的MVC2项目。当Details页面第一次运行时,一切都很棒 - 所有的javascript和jquery都运行得很好。但是,当分页运行时(使用PaginatedList和NerdDinner教程),第二页不会加载jquery对象。 (JQuery =“undefined”)。

我不知道为什么第二页不起作用。我已经将解决​​方案减少到以下最小代码以显示行为:

Master.aspx页面:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!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 runat="server">
<title>
    <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>

<script src="../../Scripts/jquery-1.4.2.min.js" 
            type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
    $(function () {
        alert('MasterPage');
    });
</script>

<%-- scripts from child pages get inserted here --%>
<asp:ContentPlaceHolder ID="ScriptContent" runat="server" />
</head>
<body>
<div id="main">
    <asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</body>

Details.aspx页面:

<%@ Page Title="" Language="C#" 
         MasterPageFile="~/Views/Shared/Site.Master"     
         Inherits="System.Web.Mvc.ViewPage<WidgetViewModel>" %>

<asp:Content ID="TitleContent" 
             ContentPlaceHolderID="TitleContent" 
             runat="server">
The Title
</asp:Content>

<asp:Content ID="Content3" 
             ContentPlaceHolderID="ScriptContent" 
             runat="server">

<script type="text/javascript" language="javascript">
    alert('DetailsPage');
</script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%-- No Content --%>
</asp:Content>
  • JQuery库是我加载的唯一库。
  • 上面显示的脚本是我唯一的脚本。

详细信息来源/ {somequery}(两个警报都按预期显示)

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

The Title

</title>

<script src="../../Scripts/jquery-1.4.2.min.js" 
            type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
    $(function () {
        alert('MasterPage');
    });
</script>


<script type="text/javascript" language="javascript">
    alert('DetailsPage');
</script>
</head>

<body>
<div id="main"></div>
</body>
</html>

详细信息的源代码/ {somequery} / 1(只有Details.aspx警报有效)

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

The Title

</title>

<script src="../../Scripts/jquery-1.4.2.min.js" 
            type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
    $(function () {
        alert('MasterPage');
    });
</script>


<script type="text/javascript" language="javascript">
    alert('DetailsPage');
</script>
</head>

<body>
<div id="main"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我同意@Josh的评论,因为根据你的路线相对路径会发生变化......让它绝对,或者更好地使用jQuery的CDN版本:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" 
    type="text/javascript" language="javascript"></script>