John
John

Reputation: 89

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?

Upvotes: 0

Views: 4591

Answers (2)

Liknes
Liknes

Reputation: 195

Looks like ViewBag.filter is never set and your paging will therefor not work.

Here's a great tutorial that will answer your question in detail: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

Upvotes: 1

Kushal Vora
Kushal Vora

Reputation: 342

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

Upvotes: 1

Related Questions