jason
jason

Reputation: 7164

Pagination not working correctly in ASP.NET MVC 5

I have a page that enables user to filter results using various filters. When I use a filter (for example writer), I get pagination as well. But when I click page 2, pagination works like there is no filter. How can I fix it? Thanks. Here's my view :

@using (Html.BeginForm("Reporting", "Book",FormMethod.Get))
{
    <p>
        <table>
            <tr>
                <td>
                    Publication Name : @Html.TextBox("nameSearch", ViewBag.CurrentFilter as string)
                    <input type="submit" value="Search in names" />
                </td>
            </tr>
            <tr style ="background-color:white">
                <td>
                    Writer : @Html.TextBox("writerSearch", ViewBag.CurrentFilter as string)
                    <input type="submit" value="Search in writers" />
                </td>
            </tr>
            <tr style="background-color:white">
                <td>
                    Publication No : @Html.TextBox("publicationNoSearch", ViewBag.CurrentFilter as string)
                    <input type="submit" value="Search in publication no" />
                </td>
            </tr>
            <tr style="background-color:white">
                <td>
                    Is On Sale : @Html.DropDownList("isOnSale", new List<SelectListItem>
                                     {
                                         new SelectListItem {Text = "", Value=null},
                                         new SelectListItem {Text = "Yes", Value="Yes"},
                                         new SelectListItem {Text = "No", Value="No"},

                                     }, ViewBag.CurrentFilter as string)
                    <input type="submit" value="List" />
                </td>
            </tr>

        </table>

    </p>
}

<table style="border : 1px solid black;">
    <tr style="border : 1px solid black;">
        <th style="border : 1px solid black;">
            Publication No
        </th>
        <th style="border : 1px solid black;">
        Writer
        </th>
        <th style="border : 1px solid black;">

            Publication Name

        </th>

    </tr>

@foreach (var item in Model)
{


    <tr style="border : 1px solid black;">
        <td style="border : 1px solid black;" width="100">
            @Html.DisplayFor(modelItem => item.PublicationNo)
        </td>
        <td style="border : 1px solid black;" width="290">
            @if (item.Yazari != null)
            {
                @Html.DisplayFor(modelItem => item.Writer)
            }
        </td>

        <td style="border: 1px solid black; font-style: italic;" width="360">
            @Html.DisplayFor(modelItem => item.PublicationName)
        </td>
</table>

<br/>

Page :  @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) / @Model.PageCount

<div class="yui3-cssreset">
    @Html.PagedListPager(Model, page => Url.Action("Reporting",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>

And this is my controller :

public ActionResult Reporting(string nameSearch, string writerSearch, string publicationNoSearch,string isOnSale , string CurrentFilter, int? page)
{

    if (nameSearch != null)
    {
        page = 1;
    }
    else
    {
        nameSearch = CurrentFilter;
    }

    ViewBag.CurrentFilter = nameSearch;

    var books = from b in db.Book select b;


    if (!String.IsNullOrEmpty(isimSearch))
    {
        books = books.Where(b => b.PublicationName.Contains(nameSearch));
    }
    else if (!String.IsNullOrEmpty(writerSearch))
    {
        books = books.Where(b => b.Writer.Contains(writerSearch));
    }
    else if (!String.IsNullOrEmpty(publicationNoSearch))
    {
        int publicationNoForSearch;
        publicationNoForSearch = Int32.Parse(publicationNoSearch);
        books = books.Where(b => b.PublicationNo == publicationNoForSearch);
    }
    else if (isOnSale == "Yes")
    {
        books = books.Where(b => b.isOnSale == true);
    }
    else if (isOnSale == "No")
    {
        books = books.Where(b => b.isOnSale == false);
    }
    else
    {
        ;
    }

    books = books.OrderBy(b => b.PublicationNo);
    int pageSize = 50;
    int pageNumber = (sayfa ?? 1);

    return View(books.ToPagedList(pageNumber, pageSize));
}

Upvotes: 2

Views: 1041

Answers (1)

user3559349
user3559349

Reputation:

You need to add the values to your Url.Action() in the Html.PagedListPager() method. Currently you only add values for sortOrder and currentFilter (and sortOrder is not even a parameter in the Reporting() so that is a bit pointless).

In your method you need to set ViewBag properties for writerSearch, publicationNoSearch etc in the same way you are currently doing for CurrentFilter,and then modify the Url.Action() to add the values, for example

@Html.PagedListPager(Model, page => Url.Action("Reporting",
    new { page, currentFilter = ViewBag.CurrentFilter, writerSearch = ViewBag.WriterSearch, publicationNoSearch = ViewBag.PublicationNoSearch, etc })

Upvotes: 1

Related Questions