ASP.NET MVC分页,保留当前查询参数 [英] ASP.NET MVC Paging, Preserving Current Query Parameters

查看:212
本文介绍了ASP.NET MVC分页,保留当前查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含联系信息的网格,我需要能够对其进行分页.

I have a grid with contact information which I need to be able to page through.

所有管道均已安装到位,最后一个细节.分页是通过简单的p Querystring参数完成的,例如www.myurl.com/grid?p=3将是第三页;资源库将自动获取正确的数据以及项目总数.每个页面的大小在其他地方定义,我不必担心查询字符串中的大小.

All the plumbing is already in place, with one last detail. Paging is done through a simple p Querystring parameter, e.g. www.myurl.com/grid?p=3 would be the third page; the repository is automatically fetching the right data, and also the total count of items. The size of each page is defined somewhere else and I don't need to worry about that in the query string.

但是,我也支持搜索等.在我的查询字符串中以q表示的搜索词.因此,现在我可以使用一个组合:www.myurl.com/grid?q=tom&p=2,它搜索"tom"并拉出第二页结果.

However, I support searching etc. as well. The search term searched for in denoted as q in my Querystring. So now I can have a combination: www.myurl.com/grid?q=tom&p=2 which searches for "tom" and pulls the second page of results.

我现在面临的问题,因为查询字符串中可能存在q(或其他)参数 ,我如何为此创建寻呼机(需要保持原始参数,因此,如果我单击第2页",则需要从

The problem I face now, since the q (or other) parameters may be present in the query string, how do I create a pager for this (which would need to keep the original parameters, so if I click on "page 2" it needs to go from

  • www.myurl.com/grid?a=1&b=xyz&q=tom

  • www.myurl.com/grid?a=1&b=xyz&q=tom

www.myurl.com/grid?a=1&b=xyz&q=tom&p=2

www.myurl.com/grid?a=1&b=xyz&q=tom&p=2

我该怎么做?

推荐答案

昨天我问了类似的问题.也许你想退房 在.net mvc中保留数据

I asked a similar question yesterday. Maybe you want to check out Preserve data in .net mvc

以下是从史蒂夫·桑德森(Steve Sanderson)的

following is the code copied from Steve Sanderson's book

public static class PagingHelpers
{
    public static string PageLinks(this HtmlHelper html, int currentPage,
    int totalPages, Func<int, string> pageUrl)
    {
        StringBuilder result = new StringBuilder();
        for (int i = 1; i <= totalPages; i++)
        {
            TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag

            tag.MergeAttribute("href", pageUrl(i));
            tag.InnerHtml = i.ToString();
            if (i == currentPage)
                tag.AddCssClass("selected");


            result.AppendLine(tag.ToString());
        }
        return result.ToString();
    }
}

这篇关于ASP.NET MVC分页,保留当前查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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