1
我有一個可以搜索的項目列表和頁面有分頁。當輸入搜索查詢時,部分視圖會正確加載(AJAX)。然而,當你使用分頁時,它會觸發一個非ajax請求(整個頁面被重新加載並且頭部沒有XmlHttpRequest標誌)。AJAX請求不被識別爲
是什麼原因造成的?
$(function() {
// Adds Ajax to pagination of search results
var getPage = function() {
var $a = $(this);
var options = {
url: $a.attr("href"),
type: "get",
data: $("form").serialize()
};
$.ajax(options).done(function (data) {
var $target = $a.parents("div.pagedlist").attr("data-nn-target");
var $newHtml = $(data);
$target.html($newHtml);
$newHtml.effect("highlight");
});
// Prevent default action
return false;
};
$(".main-content").on("click", ".pagedlist a", getPage);
});
<form method="GET" action='@Url.Action("Index", "Show")' data-nn-ajax="true" data-nn-target="#contentlist" class="form-search">
<div class="input-append mysearch">
<input type="search" class="span5 search-query" name="query" data-nn-autocomplete="@Url.Action("AutoComplete")" />
<input type="submit" class="btn" value="Search" />
</div>
</form>
<div id="contentlist">
<table></table> // content
<div class="pagedlist" data-nn-target="#contentlist">
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }), PagedListRenderOptions.MinimalWithItemCountText)
</div>
</div>
根據教程,這行代碼應該已經解決了不能使用分頁結果集的問題。
data: $("form").serialize()
public ActionResult Index(string query = "", int page = 1) {
IPagedList<ShowViewModel> model;
if (Request.IsAjaxRequest()) {
model = ViewModelFactory.Instance.CreateShowViewModels(_showRepository.GetShows()
.Where(
x =>
x.Title.ToLower()
.Contains(
query.ToLower())))
.OrderByDescending(x => x.LatestRelease).ToList().ToPagedList(page, 15);
ViewBag.Search = query;
return PartialView("_Shows", model);
}
model = ViewModelFactory.Instance.CreateShowViewModels(_showRepository.GetShows()).OrderByDescending(x => x.LatestRelease).ToList().ToPagedList(page, 15);
ViewBag.Search = null;
return View(model);
}
當我把一個斷點,同時在View
和PartialView
呼叫,它擊中每當我使用分頁(從而導致每次都顯示的所有數據)View
。爲什麼我的請求不被視爲AJAX?
您是否使用此項目中的PagedList? https://github.com/TroyGoode/PagedList – Jacob
@Jacob:是的,我是。 –
關閉'});'爲'document.ready'事件處理程序後,額外的'}'是多少? – Jasper