Message paging issue in ASP.NET MVC

I have a page that takes several parameters in the form and sends them to the action. It returns a series of search results that need to be done through the pages. My pager uses ActionLink;

<%= Html.ActionLink(i.ToString(), "Basic", new { page = (i - 1) })%>

The results are returned as expected, but when I click on the second page, it goes to the default action, and not to the message marked with the message. Form values ​​are not returned repeatedly, and the results shown on the second page are the default results that are not filtered with parameters.

I am not sure how to solve this problem? One way was to store the form values ​​in a database in the mail and read them by default, but that seems redundant.

Thank!

+3
source share
2 answers

The MVCContrib network and pager deals with this particular scenario. You can write your own, but I would recommend both the Grid and Pager interface helpers.

http://mvccontrib.codeplex.com/

http://mvccontrib.codeplex.com/sourcecontrol/network/Show?projectName=MVCContrib&changeSetId=4112aa6f6d84#src%2fMVCContrib%2fUI%2fPager%2fPager.cs

private string CreateQueryString(NameValueCollection values)
        {
            var builder = new StringBuilder();

            foreach(string key in values.Keys)
            {
                if(key == _pageQueryName)
                    //Don't re-add any existing 'page' variable to the querystring - this will be handled in CreatePageLink.
                {
                    continue;
                }

                foreach(var value in values.GetValues(key))
                {
                    builder.AppendFormat("&amp;{0}={1}", key, HttpUtility.HtmlEncode(value));
                }
            }

            return builder.ToString();
        }
+2
source

I came across the same scenario. And I choose paging with ajax form messages when the user clicks on the "previous" or "next" link.

The javascript function does this.

function searchResultsPage(pageNum) 

{

$("#searchResultPageNum").val(pageNum);

var frm = $("form#ajaxSearch");
$.post(frm.attr('action'),
        frm.serialize(),
        function(rData) {
            $("#rvwLstFrmContainer").html(rData);
        });

}

Where 'searchResultPageNum' is a hidden field that reflects the new page number to load.

'ajaxSearch' - ajax, Html- AjaxForm.

'rvwLstFrmContainer' - div, .

Community! , . .

+1

Source: https://habr.com/ru/post/1742381/


All Articles