【问题标题】:How do I do a Pagination on a httpPost ActionResult如何对 httpPost ActionResult 进行分页
【发布时间】:2017-12-04 18:13:09
【问题描述】:

我对 mvc 很陌生,我在从搜索结果中对列表进行分页时遇到问题。这是我迄今为止尝试过的:这是搜索业务逻辑:

  public IQueryable<IGrouping<int, LLATTRDATA>> GetDocuments(DocumentSearchInputModel searchInputModel)
        {
            try
            {
                _entities = new Entities();
                Logger.Info("connection to db successfull" + _entities);
            }
            catch (Exception e)
            {
              Logger.Error(e);
            }

            if (_entities != null)
            {

                var result = _entities.LLATTRDATAs.AsQueryable();

                //Group Ids together
                var ids = _entities.LLATTRDATAs.Where(r => r.VALSTR.Contains(searchInputModel.OwnersName) && r.ATTRID == 2)
                .Select(r => r.ID);
                Logger.Debug("the Ids are "+ids);

                var selected = _entities.LLATTRDATAs.Where(r => ids.Contains(r.ID)).GroupBy(r => r.ID);
                Logger.Debug("the selected Ids are "+ selected);


                if (searchInputModel != null)
                {
                    //result =
                    foreach (var selectedId in selected)
                    {
                        foreach (var item in selectedId)
                        {
                            item.DATAID = (
                               from l
                                   in _entities.LLATTRDATAs
                               join d
                                   in _entities.DTREEs
                                   on l.ID
                                   equals d.DATAID
                               join v
                                   in _entities.DVERSDATAs
                                   on d.VERSIONNUM
                                   equals v.VERSION
                               where d.DATAID == v.DOCID && l.ATTRID == 2
                                     && l.VALSTR.Contains(searchInputModel.OwnersName) && l.VERNUM == v.VERSION
                               select l.ID).ToList().FirstOrDefault();

                            Logger.Info("DataID is "+ item.DATAID);

                            PROVIDERDATA providerData = (
                                from p
                                    in _entities.PROVIDERDATAs
                                join v
                                    in _entities.DVERSDATAs on p.PROVIDERID
                                    equals v.PROVIDERID
                                where v.DOCID == item.DATAID && v.VERSION == 1
                                select p).FirstOrDefault();
                            Logger.Info("provider data is "+ providerData);

                            //Get the needed string from the full path
                            Regex regexForUsefulUrl = new Regex("(?<==')(.*)(?=','st)", RegexOptions.Singleline);
                            if (providerData != null)
                            {
                                var getUsefulUrl = regexForUsefulUrl.Matches(providerData.PROVIDERDATA1);
                                var useFulUrl = getUsefulUrl[0].Value;
                                Logger.Debug("Needed URL is:", new Exception(useFulUrl));
                                item.FILECONTENT = System.Configuration.ConfigurationManager.AppSettings["ServerUrl"] + useFulUrl;
                            }
                        }
                    }

                   // if (getFileUrl != null) searchInputModel.FileUrl = getFileUrl.providerType;
                }
                Logger.Info($"Results found {result}");

                return selected;
            }
            return null;
        }

现在这是搜索控制器:

 public ActionResult Index()
    {
        if (Request.QueryString != null && Request.QueryString.Count > 0)
        {
            return View();
        }
        return null;

    }

    [HttpPost]
    [HandleError]
    public ActionResult Index(DocumentSearchInputModel searchInputModel, int page = 0)
    {
        _entities = new Entities();
        const int pageSize = 2;

        var business = new SearchBusinessLogic();
        var model = business.GetDocuments(searchInputModel);

        var count = model.Count();

        var data = model.OrderBy(i => i.Key).Skip(page * pageSize).Take(pageSize).ToList();
        //TempData["data"] = data;

        TempData["data"] = data;
        Session.Add("data", data);

        ViewBag.MaxPage = (count / pageSize) - (count % pageSize == 0 ? 1 : 0);

        ViewBag.Page = page;


        //save user search inputs to the db
        using (var db = new LagosOnlineESearchEntities())
        {
            var pin = TempData["UserPin"];
            var result = db.UserSearchInformations.SingleOrDefault(b => b.ReceiptNumber == (string)pin);
            if (result != null)
            {
                if (searchInputModel != null)
                {
                    try
                    {
                     result.SearchByOwnersName = searchInputModel.OwnersName;
                                            result.SearchByOwnersAddress = searchInputModel.OwnersAddress;
                                            result.SearchByVolumeNumber = searchInputModel.VolumeNumber;
                                            result.SearchBySurveyPlanNumber = searchInputModel.SurveyPlanNumber;
                                            result.SearchByDescriptionOrLocationOfProperty = searchInputModel.DescriptionOrLocationOfProp;
                    }
                    catch (Exception e)
                    {

                        Logger.Error("Error", e);
                    }

                }
                Logger.Info("Details about to be to save");
                db.SaveChanges();
                Logger.Info("User search inputs saved to the db");
            }
        }
        if (!ModelState.IsValid) {

            return View(searchInputModel);
        }
        return View("searchResult", data);

    }

注意:HttpGet 索引显示搜索输入字段(它是多搜索)

这是我的观点(我遇到问题的部分):

 @if (ViewBag.Page > 0)
                {
                    <a href="@Url.Action("Index", "Search", new {page = ViewBag.Page  - 1})" class="btn btn-danger btn-fill">&laquo; PREV</a>
                }

                @if (ViewBag.Page < ViewBag.MaxPage)
                {
                    <a href="@Url.Action("Index", "Search", new {page = ViewBag.Page  + 1})" class="btn btn-danger btn-fill">NEXT &laquo;</a>
                }

因此,如果我单击“下一步”,它会将我带到 httpGet 搜索索引(搜索输入页面),而不是剩余的搜索结果。请问我该怎么做?

谢谢

我刚刚找到了另一种可能的解决方案,方法是添加一个新的 actionResult 方法并将所有代码从执行分页的 httpPost 索引移动到此方法,如下所示:

 [HttpGet]
    public ActionResult SearchResult(int page = 0)
    {
        DocumentSearchInputModel searchInputModel = (DocumentSearchInputModel) TempData["data"];

        _entities = new Entities();
        const int pageSize = 2;

        var business = new SearchBusinessLogic();
        var model = business.GetDocuments(searchInputModel);

        var count = model.Count();

        var data = model.OrderBy(i => i.Key).Skip(page * pageSize).Take(pageSize).ToList();

        ViewBag.MaxPage = (count / pageSize) - (count % pageSize == 0 ? 1 : 0);

        ViewBag.Page = page;

        return View("searchResult", data);
    }

但 searchInputModel 返回 null 而不是用户输入,我尝试了 tempData、viewbag 和 viewdata,但似乎都没有工作。所以我现在的挑战是如何将用户输入从那个索引 httppost 传递给新的 actionresult 方法。谢谢各位...

【问题讨论】:

    标签: asp.net-mvc pagination


    【解决方案1】:

    要回答这个问题:“但是 searchInputModel 返回 null 而不是用户输入,我尝试了 tempData、viewbag 和 viewdata,但似乎都没有工作。”只是因为在调用请求之前,您还没有将其值设置为 TempData、ViewBag 或 ViewData。

    所以,我的建议是在这部分添加 get 和 set 代码:

        @if (ViewBag.Page > 0)
            {
                //To get and set user inputs here
                var searchInputModel = new DocumentSearchInputModel();
                ...
                TempData["data"] = searchInputModel;
                <a href="@Url.Action("Index", "Search", new {page = ViewBag.Page  - 1})" class="btn btn-danger btn-fill">&laquo; PREV</a>
            }
    
    @if (ViewBag.Page < ViewBag.MaxPage)
            {
               //To get and set user inputs here
                var searchInputModel = new DocumentSearchInputModel();
                ...
                TempData["data"] = searchInputModel;
                <a href="@Url.Action("Index", "Search", new {page = ViewBag.Page  + 1})" class="btn btn-danger btn-fill">NEXT &laquo;</a>
            }
    

    但是,我认为您最好先看看一些教程,然后再编写此分页。这个例子很好,很容易理解:https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

    【讨论】:

    • 非常感谢!...获取和设置输入解决了这个问题。
    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多