Search code examples
c#asp.netentity-frameworkpaginationfiltering

ASP.NET MVC. PagedList filtering by multiple params


I have a web page with filter params and created simple class to proceed that params by Where() method.

But there is one problem to pass the params by Html.Action.

Search Options:

public class Document
{
    public string Name { get; set; }
    public string Title { get; set; }
    public string Param1{ get; set; }
    public string Param2{ get; set; }
}

Controller Action

 public ActionResult Index(int? page, Document filter)
    {
        ViewBag.Params= documentFilter;
        IEnumerable<Document> DocumentList = dbContext.Documents.ToList();
        DocumentList = DocumentList.CustomFilter(documentFilter);
        var pageNumber = page ?? 1;
        var onePageOfProducts = DocumentList.ToPagedList(pageNumber, 50);
        ViewBag.Documents = onePageOfProducts;
        return View();
    }

The filtering works good, but if you use paging control, params will lose.

Paging helper:

@Html.PagedListPager((IPagedList)ViewBag.Documents, 
page => Url.Action("Index", new { page , filter = (Document)ViewBag.filter}))
//Also tried Model instead of ViewBag.filter

I couldn't pass the params to action control.

Is there any way to do this?


Solution

  • You need to use ajax call instead of helper. Write ajax call and pass parameters as post method.