2009-09-22 50 views
2

运行jQuery脚本我有一个负载执行jQuery脚本的ASP.net MVC页。 该脚本在控制器上调用一个动作并保存下拉列表。页面不负载

这个工程我的开发计算机上,但在部署到Web服务器(赢2K3机器上运行IIS 6)在页面加载,但它不运行造成空下拉列表中的脚本。

我在脚本文件夹中的jQuery的1.3.2.js文件,我已经添加了映射ASPNET_ISAPI.DLL在web服务器上。还有什么我失踪?

这是页面的一部分,可以保护在我的机器上工作的下拉列表,但不在其部署的web服务器上,因为您可以看到该脚本调用ApplicationSettings控制器以获取JSON对象,以保护下拉列表

<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server"> 
    <script src="~/Scripts/jquery-1.3.2.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     // Wait for the document to be ready 
     $(document).ready(function() 
     { 
      var selectedApp = $('#selectedApplication').val(); 
      var selectedMac = $('#selectedMachine').val(); 

      // Get the list of applications and populate the applications drop down list 
      $.getJSON("/ApplicationSettings/Applications/List", function(data) 
      { 
       var items = "<option>----------- Select Application to Configure ----------</option>"; 
       $.each(data, function(i, application) 
       { 
        var selected = (application.Value == selectedApp) ? 'selected' : ''; 
        items += "<option value='" + application.Value + "'" + selected + ">" + application.Text + "</option>"; 
       }); 
       $("#Applications").html(items); 
      }); 

      // Get the list of machines where the selected application is installed and populate the machines drop down list 
      $("#Applications").change(function() 
      { 
       if ($("#Applications").attr("value") != "") 
       { 
        // Enable the Machines DDL if a valid application is selected 
        $("#Machines").removeAttr("disabled"); 

        // Populate the machines DDL with a list of machines where the selected application is installed 
        $.getJSON("/ApplicationSettings/Machines/List/" + $("#Applications > option:selected").attr("value"), function(data) 
        { 
         // Set the first item in the list 
         var items = "<option>---------- Select Machine -----------</option>"; 

         // Retrieve the list of machines for th selected application from the database 
         $.each(data, function(i, machine) 
         { 
          var selected = (machine.Value == selectedMac) ? 'selected' : ''; 
          items += "<option value='" + machine.Value + "'" + selected + ">" + machine.Text + "</option>"; 
         }); 

         // Add the items retrieved to the Machines DDL 
         $("#Machines").html(items); 

         if ($("#Machines").attr("value") != "") 
         { 
          $("#btnSearch").removeAttr("disabled"); 
         } 
         else 
         { 
          $("#btnSearch").attr("disabled", "disabled"); 
         } 
        }); 
       } 
       else 
       { 
        // If a valid application has not been selected then disable the Machines DDL 
        $("#Machines").attr("disabled", "disabled"); 
        $("#btnSearch").attr("disabled", "disabled"); 
       } 
      }); 

      if (selectedApp != "") 
      { 
       $("#Machines").removeAttr("disabled"); 

       $.getJSON("/ApplicationSettings/Machines/List/" + selectedApp, function(data) 
       { 
        var items = "<option>---------- Select Machine -----------</option>"; 
        $.each(data, function(i, machine) 
        { 
         var selected = (machine.Value == selectedMac) ? 'selected' : ''; 
         items += "<option value='" + machine.Value + "'" + selected + ">" + machine.Text + "</option>"; 
        }); 
        $("#Machines").html(items); 
       }); 

       if (selectedMac != "") 
       { 
        $("#btnSearch").removeAttr("disabled"); 
       } 
       else 
       { 
        $("#btnSearch").attr("disabled", "disabled"); 
       } 
      } 
      else 
      { 
       $("#Machines").attr("disabled", "disabled"); 
       $("#btnSearch").attr("disabled", "disabled"); 
      } 
     }); 


     function saveSelectedApplication() 
     { 
      $("#selectedApplication").val(""); 
      $("#selectedMachine").val(""); 
      $("#selectedApplication").val($("#Applications").attr("value")); 
      if ($("#Applications").attr("value") != "") 
      { 
       $("#Machines").removeAttr("disabled"); 
       if ($("#Machines").attr("value") != "") 
       { 
        $("#btnSearch").removeAttr("disabled"); 
       } 
       else 
       { 
        $("#btnSearch").attr("disabled", "disabled"); 
       } 
      } 
      else 
      { 
       $("#Machines").attr("disabled", "disabled"); 
       $("#btnSearch").attr("disabled", "disabled"); 
      } 
     } 

     function saveSelectedMachine() 
     { 
      $("#selectedMachine").val(""); 
      $("#selectedMachine").val($("#Machines").attr("value")); 
      if ($("#Machines").attr("value") != "") 
      { 
       $("#btnSearch").removeAttr("disabled"); 
      } 
      else 
      { 
       $("#btnSearch").attr("disabled", "disabled"); 
      } 
     } 
    </script> 
+0

您的脚本是否在运行?脚本顶部的简单提醒? – littlechris 2009-09-22 19:58:09

+0

你在你的global.asax文件中使用任何自定义路由吗?您在下面留下的评论与我在实施如下之前得到的错误是一样的。我相信由于我的脚本路径问题,JS运行得太早。 – littlechris 2009-09-22 20:24:41

+0

剧本是有定位的作用在该行 $ .getJSON( “/的applicationSettings /应用/列表” 中输入功能(数据) 一个问题,如果我将其更改为\t \t \t $ .getJSON(” ../的applicationSettings /应用程序/列表“,功能(数据) 它的工作原理 我想我真的需要做的是能够正确地解决行动的路径不知道如何做到这一点在jquery 任何想法? – user99513 2009-09-22 20:58:50

回答

5

我有剧本寻路的问题。我用这个扩展方法对它进行排序。

public static class HtmlHelperExtensions 
    { 
     /// <summary> 
     /// Scripts the specified HTML to allow for correct pathing of the resource. 
     /// </summary> 
     /// <param name="html">The HTML.</param> 
     /// <param name="path">The path.</param> 
     /// <returns></returns> 
     public static string Script(this HtmlHelper html, string path) 
     { 
      var filePath = VirtualPathUtility.ToAbsolute(path); 
      return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>"; 
     } 
    } 

然后把这个母版页:

<%@ Import Namespace="MYNAMESPACE.Helpers" %> 

,然后仅仅指刚登记像所有的脚本:

<%=Html.Script("~/Scripts/jquery-1.3.2.min.js")%> 

尝试implemeting的follwoing助手以及:

public static string AbsolutePath(this HtmlHelper html, string path) 
{ 
    return VirtualPathUtility.ToAbsolute(path); 
} 

然后改变你的c全部为

$.getJSON("<%=Html.AbsolutePath("~/ApplicationSettings/Machines/List/")%>" 

当您的视图呈现时,绝对路径应由MVC ViewEngine插入。

+0

+1尼斯简单的例子。 – CmdrTallen 2009-09-22 19:54:29

+0

littlechris这个作品!非常感谢你的帮助。并解释了解决方案。 我做了细微的变化,即增加了“〜/”来获得的getJSON方法工作 \t \t $ .getJSON(“<%= Html.AbsolutePath(”〜/ ApplicationSettings/Machines/List /“)%>” – user99513 2009-09-23 13:23:11

0

你把你的代码放在“$(document).ready(function(){[YOUR_CODE]});”块?如果不是,DOM可能还没有准备好;)

+0

正如你所看到的,我确实有一个$(document).ready(....)块内的函数。 我走到直通脚本使用提供IE8的脚本调试工具,它抛出一个错误说“对象应为”在该行 $(文件)。就绪(函数().. 任何想法? – user99513 2009-09-22 20:11:52

+0

这可能是一个脚本路径问题就像littlechris指出的那样,看起来像$没有被定义(这是唯一可能在这一点上未定义的东西)在附注中,我强烈建议使用firefox + firebug进行正确的调试(IE8的是一个悲伤的玩笑)。 – 2009-09-22 21:06:51

0

你是如何编码的动作?如果您在服务器上的虚拟目录中运行,这可能很重要。如果你作为http://localhost:xxxx/controller/action在本地运行,但由于远程运行http://mysever/myapp/controller/action,那么你需要确保你使用Url.Action()解决的行动结果的真实路径。