问题
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