asp.net mvc 分页用户控件

来源:互联网 发布:淘宝的店铺号有什么用 编辑:程序博客网 时间:2024/05/21 09:51

用户控件  Controller  --代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class pagerController : Controller
    {
        //
        // GET: /pager/

        public ActionResult Index(int thispid, int allcount,string url)
        {
            string str="<a href=/"" +url+ "/">首页</a>&nbsp;<a href=/"" +url+ "?pid=" + (thispid - 1).ToString() + "/">上一页</a>";         
            int p = 0;
            if ((allcount - thispid) > 5)
            {
                p = thispid- 5;
            }
            else
            {
                p = thispid-(10 - (allcount - thispid));
            }
            if (p < 1)
            {
                p = 1;
            }
            int p2 = p + 10;
            if (p2 > allcount + 1)
            {
                p2 = allcount + 1;
            }
             for(int z=p;z<p2;z++)
             {
                 if(z==thispid)
                 {
                     str+=  "&nbsp;<a>" + z.ToString() + "</a>&nbsp;";
                 }else
                 {
                    str+=  "&nbsp;<a href=/"" +url+ "?pid=" + z.ToString() + "/">[" + z.ToString() + "]</a>&nbsp;";
                 }
             }
             str += "<a href=/"" + url + "?pid=" + (thispid + 1).ToString() + "/">下一页</a><a href=/"" + url + "?pid=" + allcount.ToString() + "/">尾页</a>";
            ViewData["v1"] = str;       
            return PartialView();
        }

    }
}

用户控件view--代码

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%=ViewData["v1"] %>

调用用户控件

<%Html.RenderAction("Index", "pager", new { thispid = (int)ViewData["thispid"], allcount = (int)ViewData["pagecount"],url=Url.Action("userlist","user") }); %>

这里用户控件带三个参数第一个参数:当前页,第二个参数:总页数,第三个参数:页面的url 输出效果

 

原创粉丝点击