我正在構建使用Visual Studio 2015的ASP.NET MVC 5應用程序。搜索在第一次嘗試時工作正常,但如果點擊頁面中的任何頁碼MVC PagedList component,它拋出一個Internal Server Error
。這裏是AJAX表單;注意,從搜索接收到的數據傳送到一個局部視圖:在部分視圖中使用AJAX的ASP.NET MVC PagedList
@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 Using Paged List in a partial View並增加了PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing
但仍有一些東西丟失。
當我使用Chrome瀏覽控制檯面板,它有這個錯誤,當我點擊頁碼:
http://localhost:9999/MyProject/Permits/SearchCustomers?page=2 500(內部服務器錯誤)
我在做什麼錯了,當試圖與PagedList
組件進行AJAX調用?