Using ASPNET MVC, bootstrap 4. I've tried Chrome and Firefox I'm trying to manually submit my ajax request instead of using a submit button. Each time a new pagination link is click, a javascript is supposed to fire to grab the page clicked, and post to the controller. However, this javascript click handler only engages once (breakpoint on first line only gets hit once), and only on this first time will it post to server. subsequent clicks are not handled.
Controller code which handles request .....
int itemsperpage = 12;
int totalitems = items.Count();
int totalimagescurrentpage = 0;
if (formcollection["paging"] != null)
{
try
{
paging = System.Convert.ToInt32(formcollection["paging"]);
}
catch (Exception e)
{
loggerwrapper.PickAndExecuteLogging("cannot convert pagingvaluefromform to integer");
}
}
if ((itemsperpage * paging) > totalitems)
{
totalimagescurrentpage = totalitems;
}
else
{
totalimagescurrentpage = itemsperpage;
}
int skip = (paging - 1) * itemsperpage;
items = items.OrderBy(item => item.CreationDate).Skip(skip).Take(totalimagescurrentpage);
//-------------------------
//items needed to render _PostCards
ViewBag.itemsperpage = itemsperpage;
ViewBag.totalitems = totalitems;
ViewBag.paging = paging;
//--------------------------
ViewBag.detailsview = true;
ViewBag.BrowsingUserId = (string)System.Web.HttpContext.Current.Session["UserId"];
if (Request.IsAjaxRequest())
{
return PartialView("_PostCards", await items.ToListAsync());
}
else
return View(await items.ToListAsync());
javascript that only fires once
$(document).ready(function () {
$(".page-link").click(function (event) {
var pageid = event.target.id;
var lastChar = pageid.substr(pageid.length - 1);
$("#paging").val(lastChar);
$("#pagingform").trigger('submit');
});
}); (jQuery);
HTML rendered by the view
.....
<div id="grid">
<div aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li class="page-item active"><a id="page1" class="page-link" href="#">1</a></li>
<li class="page-item "><a id="page2" class="page-link" href="#">2</a></li>
<li class="page-item "><a id="page3" class="page-link" href="#">3</a></li>
<li class="page-item"><a id="n2" class="page-link" href="#">Next</a></li>
</ul>
</div>
</div>
<form action="/Items/Canada/BC/cityindex/Victoria?posttypefilter=downtown" data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#grid" id="pagingform" method="post">
<input type="submit" class="ajaxformsubmit">
<input type="hidden" name="paging" id="paging" />
</form>