我遇到了为我的MVC 5项目编写的一些Ajax代码的问题,我正在尝试使用ajax为我的表实现一个搜索栏,但是我遇到了这个错误"无法加载资源:服务器响应状态为500(内部服务器错误)"。
这是我的部分视图代码,其中包含JavaScript Ajax代码:
@model IEnumerable<ToDo.Models.Venue>
@*Search Box*@
@using (Html.BeginForm())
{
<p>
<input type="text" class="form-control" id="txtSearch">
<span class="btn btn-sm btn-warning" id="btnCustomerInc" onclick="VenueSearch();">Search</span>
</p>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.VenueName)
</th>
<th>
@Html.DisplayNameFor(model => model.VenueType)
</th>
<th>
@Html.DisplayNameFor(model => model.VenueTown)
</th>
<th>
@*blank*@
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.VenueName)
</td>
<td>
@Html.DisplayFor(modelItem => item.VenueType)
</td>
<td>
@Html.DisplayFor(modelItem => item.VenueTown)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.VenueID }, new { @class = "btn btn-primary" })
</td>
</tr>
}
</table>
<script>
function VenueSearch() {
console.log("Venue Search function hit");
var search = document.getElementById("txtSearch").value;
console.log(search);
$.ajax({
type: "GET",
url: '@Url.Action("VenuesTablePartialView", "Venues")',
data: { searchString: search },
success: function (data) {
$('#VenueTable').html(data);
$('#VenueTable').fadeIn("fast")
}
});
}
</script>
这是我对部分视图的控制器操作:
[ChildActionOnly]
public ActionResult VenuesTablePartialView(string searchString)
{
var venues = from v in db.Venues
select v;
//Search
if (!String.IsNullOrEmpty(searchString))
{
venues = venues.Where(v => v.VenueName.ToUpper().Contains(searchString.ToUpper()));
}
return PartialView("_VenuesTable", venues.ToList());
}
}
答案 0 :(得分:1)
将此代码添加到system.webServer
文件
web.config
部分
<validation validateIntegratedModeConfiguration="false" />
答案 1 :(得分:0)
我能够通过删除[ChildActionOnly]
行来解决此问题,这会导致问题,因为这意味着此控制器操作只能由子请求访问。
public ActionResult VenuesTablePartialView(string searchString)
{
var venues = from v in db.Venues select v;
//Search
if (!String.IsNullOrEmpty(searchString))
{
venues = venues.Where(v => v.VenueName.ToUpper().Contains(searchString.ToUpper()));
}
return PartialView("_VenuesTable", venues.ToList());
}