PagedListPager pass additional model data
Asked Answered
L

3

5

I have the following model:

  public class PagedClientViewModel
    {
        public int? Page { get; set; }
        public PagedList.PagedList<ClientViewModel> Clients { get; set; }               
        public bool ShowAllClients { get; set; }
    }

ShowAllClients is a checkbox that I'll use to further filter the list of clients returned from the server.

 @Html.CheckBoxFor(model => model.ShowAllClients, new { id = "ShowAllClientsCheckbox" })

Here is my pager:

@Html.PagedListPager(Model.Clients, page => Url.Action("Index", new { Page = page }), PagedListRenderOptions.DefaultPlusFirstAndLast)

Both the pager and checkbox are on the same form.

The problem that I'm having is that when I change page on the pager control, the checkbox is always set to false. This is happening because in the Index action, ShowAllClients is set to false.

How would I preserve the checkbox data when changing pages?

Liborio answered 16/1, 2013 at 10:24 Comment(0)
L
8

I got this to work with the following:

 @Html.PagedListPager(Model.Clients, page => Url.Action("Index", new PagedClientViewModel { Page = page, ShowAllClients = Model.ShowAllClients }))
Liborio answered 24/1, 2013 at 11:21 Comment(0)
L
0

Best solution if found so far is:

@Html.PagedListPager(Model.Clients, page => Url.Action("Index", new { Page = page, param1="",param2="" }), PagedListRenderOptions.DefaultPlusFirstAndLast)

And yes it works.

Lacour answered 11/4, 2019 at 7:16 Comment(1)
Thanks for quick and easy solution.Pick
E
0

In my case the Model is the PagedList.IPagedList. Next to that I use a "SearchModel" which I store in the ViewBag so it can easily be passed along. All I need to update is the page number.

@Html.PagedListPager(Model, page =>
{
    ViewBag.SearchModel.Page = page;
    return Url.Action("Index", "Search", ViewBag.SearchModel);
})
Eugeniaeugenics answered 23/1, 2021 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.