简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM