2

I have the following line of code which uses html helper's PagedListPager:

@Html.PagedListPager(Model.kyc_paged_list, page => Url.Action("ClientDetails", new { id = ViewBag.ID, kyc_page = page, transaction_page = Model.transaction_page, active = "kyc" }))

When clicking on the the pager the entire page reloads. But I only want the table with id="kyc-history" to refresh.

I know how to reload the table using a JavaScript function but I don't know how to call it from the PagedListPager. Any ideas how I can call it? Let's say that the JS function is called reloadTable()

  • use jquery to ajax calls (for example), and call controller action in `reloadTable()` like here https://api.jquery.com/load/ – hamaronooo Jun 06 '22 at 09:24
  • yes that's is what I'll use. My question is how to call the function from the PagedListPager. I have spoken with the lead developer and he does not want me to change the PagedListPager – Arsenios Dracoudis Jun 06 '22 at 09:48
  • can you create button or any control witch will call function? you can create `setInterval` in `script`, witch updating your table with interval – hamaronooo Jun 06 '22 at 09:57
  • 1
    adding a button will defeat the purpose of using PagedListPager in the first place – Arsenios Dracoudis Jun 06 '22 at 10:00
  • Ok, check this https://stackoverflow.com/questions/21606507/call-javascript-function-from-pagedlistpager – hamaronooo Jun 06 '22 at 10:04

1 Answers1

0

I was having the same problem and this article (done by Anders Lybecker) helped me out and now everything working! It's very clear with steps, But make sure to make backup before starting!

Take a look: AJAX paging for ASP.NET MVC sites

In summary you make 2 Action Results in your Controler with 2 Views 1 for your page (in my case it's Index View) the other for the List that contain the PagedListPager control (List View). And put the list View inside the Index View. And write the JQuery code for the PagedListPager in the Index View.

You can read the article for the details!

And this is my code with a little extra things I noticed to help you more:

The List View:

@model IPagedList<StudentRegSys.Models.Student>
@{
    Layout = null;
}

@using PagedList.Mvc;
@using PagedList;

<link href="https://fonts.googleapis.com/css?family=Open+Sans:400italic,400,600,700" rel="stylesheet">
@Styles.Render("~/template/css")

<div class="container">
    <div class="row">

          <!-- The List Code-->

        <div class="pagination-control">
            @Html.PagedListPager(Model, i => Url.Action("List", "Home", new { i, search = Request.QueryString["search"] }))
        </div>
    </div>
</div>
@Scripts.Render("~/template/js")

Note: Make sure to make Layout = null; and put the Styles Links & the Scripts manual in this View to avoid design issues.

In the Controller: (it's Home Controller in my case)

// GET: /Home/Index
public ViewResult Index()
{
    return View();
}

// GET: /Home/List
public ActionResult List(int? i, string search = "")
{
    try
    {
        var students = _context.Student.Include(s => s.Major)
            .OrderBy(s => s.Name)
            .Where(s => s.Name.Contains(search) || s.Major.Name.Contains(search) ||
            s.Address.Contains(search) || s.Phone.Contains(search))
            .ToList().ToPagedList(i ?? 1, 8);


        return View(students);
    }
    catch (Exception)
    {
        return HttpNotFound();
    }
}

The Index View:

@{
    ViewBag.title = "Home";
}

<section id="intro">
    <!-- Some Code -->
</section>

<section id="maincontent">

    @Html.Action("List") <!-- to call the List view --> 

</section>

    <script>
    $(document).ready(function () {
        $(document).on("click", ".pagination-control a[href]", function () {
            $.ajax({
                url: $(this).attr("href"),
                type: 'GET',
                cache: false,
                success: function (result) {
                    $('#maincontent').html(result);
                }
            });
            return false;
        });
    });
    </script>

Note: Make sure to put the html(result) in the root container of the list in my case was <section id="maincontent">.

Hope this help you out :)