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

这一生的挚爱 提交于 2020-01-03 04:24:05

问题


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 bellow.

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 dont know how can i pass model in Html.PagedListPager

@Html.PagedListPager(Model, page => Url.Action("GetSearchdProperty", "SearchProperty", new { page })).

EDIT

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; }
    }

回答1:


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, .... }))


来源:https://stackoverflow.com/questions/38551527/search-model-is-getting-cleared-for-second-page-request-in-pagedlist-mvc-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!