在MVC中的PagedList.MVC中,搜索模型被清除为第二页请求

时间:2016-07-24 11:27:48

标签: jquery .net asp.net-mvc paging pagedlist

我在我的MVC应用程序中使用PagedList.MVC进行分页。首先我在主页上,当我填写搜索栏并提交表单时,它会在搜索方法上发布模型,如下所示。

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

因此,此重定向到搜索标准结果的搜索页面。现在当我点击PagedList.MVC分页的第二页时调用相同的方法,但此时我得到的SearchModel为空或说空白但是我希望我的搜索模型在所有页面请求时从分页开始。那我该怎么做呢?

我已尝试在VieBag中使用搜索模型,但我没有介绍如何在分页请求中从视图包发送模型。我不知道如何在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; }
    }

1 个答案:

答案 0 :(得分:1)

IPagedList<T>的视图模型添加属性,然后返回您的模型,以便将搜索/过滤器值添加到网址

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

并在视图中

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