在MVC中的PagedList.MVC中,第二个页面请求的搜索模型已被清除 [英] Search model is getting cleared for second page request in PagedList.MVC in MVC

查看:97
本文介绍了在MVC中的PagedList.MVC中,第二个页面请求的搜索模型已被清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PagedList.MVC在我的MVC应用程序中进行分页.所以首先我在主页上,当我填写搜索栏并提交表格时,将其张贴在搜索方法上的Model上,如下所示.

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

因此,此重定向到包含搜索到的条件结果的搜索页面.现在,当我单击PagedList.MVC分页的第二页时,将调用相同的方法,但此时我将SearchModel清空或说为空白,但我希望从分页开始时所有页面请求上的搜索模型都一样.那么我该如何实现呢?

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?

我曾尝试在VieBag中采用搜索模型,但是我没有在如何从分页请求中的视图包中发送模型的问题.我不知道如何在Html.PagedListPager

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

编辑

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

推荐答案

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

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

并在视图中

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

这篇关于在MVC中的PagedList.MVC中,第二个页面请求的搜索模型已被清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆