use*_*513 2 asp.net-mvc jquery
我有一个ASP.net mvc页面,在加载时执行jquery脚本.该脚本在控制器上调用操作并为下拉列表提供保湿.
这适用于我的开发机器但是当部署到网络服务器(运行IIS 6的Win 2k3盒子)时,页面会加载,但它不会运行脚本,从而产生一个空的下拉列表.
我在scripts文件夹中有jquery-1.3.2.js文件,我已经在webserver上添加了aspnet_isapi.dll的映射.还有什么我想念的吗?
这是页面的一部分,它可以保存在我的机器上运行的下拉列表,但不会在它部署到的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>
Run Code Online (Sandbox Code Playgroud)
我遇到了脚本路径问题.我用这个扩展方法对它进行排序.
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>";
}
}
Run Code Online (Sandbox Code Playgroud)
然后把它放在母版页中:
<%@ Import Namespace="MYNAMESPACE.Helpers" %>
Run Code Online (Sandbox Code Playgroud)
然后jsut注册所有脚本,如:
<%=Html.Script("~/Scripts/jquery-1.3.2.min.js")%>
Run Code Online (Sandbox Code Playgroud)
尝试实现以下帮助器:
public static string AbsolutePath(this HtmlHelper html, string path)
{
return VirtualPathUtility.ToAbsolute(path);
}
Run Code Online (Sandbox Code Playgroud)
然后将你的电话改为
$.getJSON("<%=Html.AbsolutePath("~/ApplicationSettings/Machines/List/")%>"
Run Code Online (Sandbox Code Playgroud)
渲染视图时,MVC ViewEngine应插入绝对路径.
| 归档时间: |
|
| 查看次数: |
1788 次 |
| 最近记录: |