momofierce
momofierce

Reputation: 89

Sorting and Paging MVC

How does sorting and paging work?

As a preface, I'm very new with the material. So layman's terms are appreciated.

I followed turorials, posted on here and have mine generally working. I'm just confused though because when I sort my column, the sorted list doesn't continue onto the next page. Then when I click the header to sort, say AppName, on the next page, it just takes me back to the first page.

How do I keep it from doing this?

public ActionResult Index(string sortOrder, string searchString, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.IDSortParm = String.IsNullOrEmpty(sortOrder) ? "AppID_desc" : "";
        ViewBag.NameSortParm = sortOrder == "Name" ? "AppName_desc" : "Name";
        ViewBag.TechSortParm = sortOrder == "Tech" ? "Technology_desc" : "Tech";
        ViewBag.VersSortParm = sortOrder == "Vers" ? "Version_desc" : "Vers";
        ViewBag.TierSortParm = sortOrder == "Tier" ? "Tier_desc" : "Tier";
        ViewBag.StratSortParm = sortOrder == "Strat" ? "Strategy_desc" : "Strat";

        if (Request.HttpMethod != "GET")
        {
            page = 1;
        }


        ViewBag.CurrentFilter = searchString;


        var applications = from a in db.Application_
              select a;

        if (!String.IsNullOrEmpty(searchString))
        {
            applications = applications.Where(a => a.AppName.ToUpper().Contains(searchString.ToUpper()));

        }

         switch (sortOrder)
         {
                case "AppID_desc":
                applications = applications.OrderByDescending(a => a.AppID);
                break;
                case "Name":
                applications = applications.OrderBy(a => a.AppName);
                break;
                case "AppName_desc":
                applications = applications.OrderByDescending(a => a.AppName);
                break;
                case "Vers":
                applications = applications.OrderBy(a => a.Version);
                break;
                case "Version_desc":
                applications = applications.OrderByDescending(a => a.Version);
                break;
                case "Tier":
                applications = applications.OrderBy(a => a.Tier);
                break;
                case "Tier_desc":
                applications = applications.OrderByDescending(a => a.Tier);
                break;
                case "Strat":
                applications = applications.OrderBy(a => a.Strategy);
                break;
                case "Strat_desc":
                applications = applications.OrderByDescending(a => a.Strategy);
                break;
                default:
                applications = applications.OrderBy(a => a.AppID);
                break;

                }
         int pageSize = 20;
         int pageNumber = (page ?? 1);
         return View(applications.ToPagedList(pageNumber, pageSize));
    }

Index after table:

<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount

@if (Model.HasPreviousPage)
{
    @Html.ActionLink("<<", "Index", new { page = 1 })
    @Html.Raw(" ");
    @Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1 })
}
else
{
    @:<<
    @Html.Raw(" ");
    @:< Prev
    }

@if (Model.HasNextPage)
{
    @Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1 })
    @Html.Raw(" ");
    @Html.ActionLink(">>", "Index", new { page = Model.PageCount })
}
else
{
    @:Next <
    @Html.Raw(" ");
    @:>>

}

If anyone has any information for me, or could point me in the right direction that would be amazing. Thanks!!

Upvotes: 1

Views: 299

Answers (2)

Emmanuel N
Emmanuel N

Reputation: 7449

You need to include sorting order ViewBag.CurrentSort in your action links Something like:

 @Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1 }, sortOrder = ViewBag.CurrentSort)

Upvotes: 2

Chris Pratt
Chris Pratt

Reputation: 239460

You have to carry whatever query params are already set with you. For the page links, for example, you need to include the sort query param when building the link:

@Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = Request["sortOrder"] })

The same goes in reverse for your sort. If you want to carry over the page number, then you need to include the page number when building the link. However, it should be noted that it's fairly typical to go to page 1 when changing the sort, since the context of the page number has changed.

Upvotes: 3

Related Questions