我最近开始在Visual Studio 2012中使用MVC4。
我有一个HomeController,它具有强类型的HomeView,在此HomeView中,我有一个局部视图,当用户从DropDownList中进行选择时会显示该局部视图,该局部视图由Ajax发布到WCF Web服务中显示。在我的开发环境中,这一切都可以正常工作,但是一旦我将项目上传到IIS,从下拉列表中进行选择时,什么都不会发生,所以我打开了开发控制台,并收到以下错误消息
404-找不到文件或目录。您正在寻找的资源可能已被删除,更名或暂时被删除不可用。
我已经阅读了很多帖子,对服务器进行了更改,并将以下内容添加到了我的web.config中
<modules runAllManagedModulesForAllRequests="true"></modules>
而且我仍然收到404错误。
这里是从HomeController内调用我的局部视图的动作。
[HttpPost]
public PartialViewResult Audits(int AuditID, int AccountID)
{
InspectWebService.AuditClient au = new InspectWebService.AuditClient();
List<InspectWebService.ct_auditGrid> model = au.getAllUserAudits(
Convert.ToInt32(Request.Cookies["UserInfo"]["UserID"]),
AuditID,
AccountID).ToList();
return PartialView("_Audits", model);
}
这是我的ajax,调用显示局部视图的动作
$('#ddlAudits').change(function () {
var a = $(this);
var b = $('#ddlAccounts')
$.ajax({
url: 'Home/Audits',
type: 'post',
data: { 'AuditID': a.val(), 'AccountID': b.val() },
datatype: 'html',
success: function (result) {
$('#DivAccounts').html(result).enhanceWithin();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(result);
}
});
});
$('#ddlAccounts').change(function () {
var a = $('#ddlAudits');
var b = $(this)
$.ajax({
url: 'Home/Audits',
type: 'post',
data: { 'AuditID': a.val(), 'AccountID': b.val() },
datatype: 'html',
success: function (result) {
$('#DivAccounts').html(result).enhanceWithin();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(result);
}
});
});
我只是不明白为什么它可以在我的开发环境中工作,但是一旦将其上传到我的服务器后就不行了。
这是因为在IIS上,它找不到您的部分视图文件。
改为使用@ Url.Action。
$('#ddlAudits').change(function () {
var a = $(this);
var b = $('#ddlAccounts')
$.ajax({
url: '@Url.Action("Audits","Home")',
type: 'post',
data: { 'AuditID': a.val(), 'AccountID': b.val() },
datatype: 'html',
success: function (result) {
$('#DivAccounts').html(result).enhanceWithin();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(result);
}
});
});