jquery.netasp.net-mvcpaginationpagedlist

Search model is getting cleared for second page request in PagedList.MVC in MVC


I am using PagedList.MVC for paging in my MVC application.So first I am on home page and when I fill search bar and submit the form it post the Model on search method, which is as below.

public ActionResult GetSearchdProperty(int? page,SearchModel objSearch)
{
        var objProperty =  db.re_advertise.OrderBy(r => r.id);
        return View("SearchedProperty", objProperty.ToPagedList(pageNumber: page ?? 1,pageSize: 2));
}

So this redirect to search page with searched criteria results. Now when I click 2nd page of PagedList.MVC paging same method is called but at this time I am getting my SearchModel Empty or say blank BUT I want my search model as is on all page request when going from paging. So how can I achieve this?

I have tried taking search model in VieBag but I am not getting on how to send model from view bag in paging request. I don't know how can I pass model in Html.PagedListPager

@Html.PagedListPager(Model, page => Url.Action("GetSearchdProperty", "SearchProperty", new { page })).
public class SearchModel
    {
        public string search_text { get; set; }
        public string min_area { get; set; }
        public string max_area { get; set; }
        public string min_price { get; set; }
        public string max_price { get; set; }
        public string bedroom { get; set; }
    }

Solution

  • Add a property to your view model for the IPagedList<T> and then return your model, so the search/filter values can be added to the url

    public class SearchModel
    {
        public string search_text { get; set; }
        public string min_area { get; set; }
        ....
        IPagedList<yourModel> Items { get; set; }
    }
    
    public ActionResult GetSearchdProperty(int? page, SearchModel model)
    {
        model.Items = db.re_advertise.OrderBy(r => r.id).ToPagedList(pageNumber: page ?? 1,pageSize: 2));
        return View("SearchedProperty", model);
    }
    

    and in the view

    @model SearchModel
    ....
    @Html.PagedListPager(Model.Items, page => Url.Action("GetSearchdProperty", "SearchProperty", 
        new { page, search_text = Model.search_text, min_area = Model.min_area, .... }))