我正在使用Visual Studio 2015构建一个ASP.NET MVC 5应用程序。搜索在第一次尝试时正常工作,但是如果我点击MVC PagedList组件中的任何一页编号,它会抛出一个内部服务器错误。这是AJAX表单;请注意,它将从搜索接收到的数据传递给一个局部视图:
在
以下是成功和失败回调函数:
我看了这个例子:MVC 4 使用 Paged List 在 partial View 中 并添加了
当我在 Chrome 中查看控制台面板时,在单击页面编号时会出现以下错误:
@using (Ajax.BeginForm("SearchCustomers", "Permits",
new AjaxOptions
{
UpdateTargetId = "targetElement",
OnSuccess = "onAjaxSuccess",
OnFailure = "onAjaxFailure"
},
new { @class = "form-horizontal form-small", role = "form", id="customerSearchForm" }))
{
@Html.AntiForgeryToken()
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4>Customer Search</h4>
</div>
<div class="modal-body">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group-sm clearfix">
@Html.LabelFor(m => m.SearchVm.SearchCustomerNameNumber, new { @class = "control-label col-xs-5 col-md-5" })
<div class="col-xs-5 col-md-5">
<div class="input-group">
@Html.EditorFor(m => m.SearchVm.SearchCustomerNameNumber, new {htmlAttributes = new {@class = "form-control"}})
<span class="input-group-btn">
<button type="submit" class="btn btn-custom-success btn-sm btn-custom-sm small-box-shadow btn-block">
Search
<i class="fa fa-search fa-lg" aria-hidden="true"></i>
</button>
</span>
</div>
@Html.ValidationMessageFor(m => m.SearchVm.SearchCustomerNameNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="modal-search" id="targetElement">
@Html.Partial("_PermitsCustomerList", Model.SearchVm.Customers)
</div>
</div>
}
在
_PermitsCustomerList
局部视图中,我有以下内容:@using PagedList
@using PagedList.Mvc
@model IPagedList<MyProject.ViewModels.CustomerViewModel>
@if (Model != null && Model.Count > 0)
{
<div class="panel panel-default data-grid data-grid-wide">
<table class="table table-hover table-striped table-bordered table-responsive">
<tr>
<th>
Customer #
</th>
<th>
Customer Name
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Customer_NO)
</td>
<td>
@Html.DisplayFor(modelItem => item.Customer_Name)
</td>
</tr>
}
</table>
<div id="contentPager">
@Html.PagedListPager(Model, page => Url.Action("SearchCustomers", "Permits",
new { page }),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( new AjaxOptions()
{
HttpMethod = "POST",
UpdateTargetId = "targetElement",
OnSuccess = "onAjaxSuccess",
OnFailure = "onAjaxFailure"
}))
</div>
</div>
}
这是控制器上的操作:
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult SearchCustomers(PermitsViewModel permitsVm, int? page)
{
if (string.IsNullOrEmpty(permitsVm.SearchVm.SearchCustomerNameNumber)) return null;
permitsVm.Page = page;
int number;
var list = int.TryParse(permitsVm.SearchVm.SearchCustomerNameNumber, out number)
? CustomerDataService.SearchCustomerByNumber(number)
: CustomerDataService.SearchCustomerByName(permitsVm.SearchVm.SearchCustomerNameNumber);
return PartialView("_PermitsCreateCustomerList", list.ToPagedList(permitsVm.Page ?? 1, 10));
}
以下是成功和失败回调函数:
成功回调函数:
失败回调函数:
function onAjaxFailure(xhr, status, error) {
$("#targetElement").html("<strong>An error occurred retrieving data:" + error + "<br/>.</strong>");
}
function onAjaxSuccess(data, status, xhr) {
if (!$.trim(data)) {
$("#targetElement").html("<div class='text-center'><strong>No results found for search.</strong></div>");
}
}
我看了这个例子:MVC 4 使用 Paged List 在 partial View 中 并添加了
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing
,但仍然缺少一些东西。当我在 Chrome 中查看控制台面板时,在单击页面编号时会出现以下错误:
当尝试使用http://localhost:9999/MyProject/Permits/SearchCustomers?page=2 500 (内部服务器错误)
PagedList
组件进行 AJAX 调用时,我做错了什么?