none
ASP.NET MVC里如何实现子导航的动态添加? RRS feed

  • 问题

  • 现有需求如下:
    网站一栏目下有子导航,要求子导航动态添加,并且有分页,在一个页面中既有该分类数据列表,又有分类列表,子导航数据存取已实现
     <%foreach (var item in Model.VideoCategories)
                  { %>
                <li>
                    <%= Html.ActionLink(Html.Encode(item.CategoryName), "Index", new { id = item.ID})%></li>
                <%} %>
    分页 route:
    routes.MapRoute(
                    "PaginateList",
                    "{controller}/Page/{page}",
                    new { controller = "", action = "Index" }
                    );
    分页这样实现:
    <%if (Model.PaginatedVideos.TotalPages > 1)
              { %>
            <a href='<%= Url.RouteUrl("PaginateList", new { page=(Model.PaginatedVideos.PageIndex-1) }) %>'>
                上一页</a>

    分类列表有分类ID,而该分类下数据列表又要分页page。
    这种情况下如何实现分类列表(子导航)动态添加?
    现在我是2者不可兼得,都晕了。。。。



    高人,MVP, where are you?
    2010年3月13日 10:26

答案

  • 你好,

    根据你的描述,大致理解如下:

    页面中有类别列表和相应的数据列表。显示哪个数据列表根据你选择哪个类别来确定。
    这个数据列表要有分页。

    这个也好办。
    我上面的代码仅仅是输出页码。
    你要把categoryid以及页面index作为参数传入action method就可以了,怎么把这两个参数传进去有两个方法。
    1.在global中添加一条路由规则以便你可以传两个参数。
    2.或者你用request.form["category"]来获得category的id之类的信息。
    至于怎么分页,如果你觉得分页很简单,可以用你自己的方法,如果有问题可以参考我的代码。

    Microsoft Online Community Support
    2010年3月15日 9:30

全部回复

  • 你好,

    可以按照下面的方式设计:
    1.分类列表用来List Categories。
    2.点击分类列表中的连接导航到该category对应的数据页面,在这页面分页。
    3.分页代码可以参考如下:然后在页面上获得Pager对象,调用CreatePager(this HtmlHelper helper,Pager pager)输出分页html代码。

    public class Pager
        {
            private int _currentPageIndex =-1;
            private int _totalPages = -1;
            private string _reqBaseUrl = string.Empty;
            private string _imgUrlForUp = string.Empty;
            private string _imgUrlForDown = string.Empty;
            private int _pagesSize=1;
            public Pager(int currentPageIndex, int totalPages, string requestBaseUrl)
            {
    
                _currentPageIndex = currentPageIndex;
                _totalPages = totalPages;
                _reqBaseUrl = requestBaseUrl;   
            }
            public Pager(int currentPageIndex, int totalPages, string requestBaseUrl, string imgUrlForUp, string imgUrlForDown)
            {
    
                _currentPageIndex = currentPageIndex;
                _totalPages = totalPages;
                _reqBaseUrl = requestBaseUrl;
                _imgUrlForDown = imgUrlForDown;
                _imgUrlForUp = imgUrlForUp;       
    
            }
            //public string CreatePager()
            //{
            //  return CustomizePager.CreatePager(this);
            //}
            public Pager(int currentPageIndex, int totalPages, string requestBaseUrl, string imgUrlForUp, string imgUrlForDown, int pagesSize)
            {
    
                _currentPageIndex = currentPageIndex;
                _totalPages = totalPages;
                _reqBaseUrl = requestBaseUrl;
                _imgUrlForDown = imgUrlForDown;
                _imgUrlForUp = imgUrlForUp;
                _pagesSize = _pagesSize > pagesSize ? _pagesSize : pagesSize;
    
            }
        
            public int CurrentPageIndex { get{return _currentPageIndex;} }
            public int TotalPages { get { return _totalPages; } }
            public string RequestBaseUrl { get { return _reqBaseUrl; } }
            public string ImageUrlForUp { get { return _imgUrlForUp; } }
            public string ImageUrlForDown { get { return _imgUrlForDown; } }
            public int PagesSize { get { return _pagesSize; } }
        }
        public static class CustomizePager
        {
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="currentPageIndex">current page index</param>
            /// <param name="totalPages">total pages</param>
            /// <param name="requestBaseUrl">base url and value construct a whole url, eg:http://baseURL/id</param>   
            public static string CreatePager(int currentPageIndex, int totalPages, string requestBaseUrl)
            {
                if (totalPages<=0)
                {
                    return string.Empty;
                }
                Validate(currentPageIndex, totalPages);
                StringBuilder sb = new StringBuilder();
                if (currentPageIndex > 0)
                {
                    sb.Append(string.Format("<a href=\"{0}\">Up</a>", requestBaseUrl + "/" + (currentPageIndex - 1).ToString()));
    
                }
                if (currentPageIndex != totalPages - 1)
                {
                    sb.Append(string.Format("<a href=\"{0}\">Down</a>", requestBaseUrl + "/" + (currentPageIndex + 1).ToString()));
                }
                return sb.ToString();
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="currentPageIndex">current page index</param>
            /// <param name="totalPages">total pages</param>
            /// <param name="requestBaseUrl">base url and value construct a whole url, eg:http://baseURL/id</param>   
            /// <param name="imageUrlForUp">image for previous page</param>
            /// <param name="imageUrlForDown">image for next page</param>
            /// <returns></returns>
            public static string CreatePager(int currentPageIndex, int totalPages, string requestBaseUrl,string imageUrlForUp,string imageUrlForDown)
            {
                if (totalPages <= 0)
                {
                    return string.Empty;
                }
                StringBuilder sb = new StringBuilder();
                Validate(currentPageIndex, totalPages);
               
                if (currentPageIndex > 0)
                {
                    sb.Append(string.Format("<a href=\"{0}\"><img src={1} style=\"boder:0px;border-color: #FFFFFF\"/></a>", requestBaseUrl + "/" + (currentPageIndex - 1).ToString(), imageUrlForUp));
    
                }
                
                if (currentPageIndex != totalPages - 1)
                {
                    sb.Append(string.Format("<a href=\"{0}\"><img src={1} style=\"boder:0px;border-color: #FFFFFF\"/></a>", requestBaseUrl + "/" + (currentPageIndex + 1).ToString(), imageUrlForDown));
    
                }
    
                return sb.ToString();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="currentPageIndex">current page index</param>
            /// <param name="totalPages">total pages</param>
            /// <param name="requestBaseUrl">base url and value construct a whole url, eg:http://baseURL/id</param>
            /// <param name="pagesSize">size of pages which are displayed at one time </param>
            /// <param name="imageUrlForUp">image for previous page</param>
            /// <param name="imageUrlForDown">image for next page</param>
            /// <returns></returns>
            public static string CreatePager(int currentPageIndex, int totalPages, string requestBaseUrl,int pagesSize, string imageUrlForUp, string imageUrlForDown)
            {
                if (totalPages <= 0)
                {
                    return string.Empty;
                }
                StringBuilder sb = new StringBuilder();
                Validate(currentPageIndex, totalPages,pagesSize);
                string href = "javascript:void(0)";
                if (currentPageIndex/pagesSize != 0)
                {
                    href = string.Format(requestBaseUrl+"/"+((currentPageIndex/pagesSize)*pagesSize-1).ToString());
                }
    
                sb.Append(string.Format("<a href=\"{0}\"><img src=\"{1}\" style=\"boder:0px;border-color: #FFFFFF\"/></a>", href, imageUrlForUp));
                sb.Append("&nbsp;&nbsp;");
                
                int pageIndexMaxIncrement = (currentPageIndex / pagesSize + 1) * pagesSize > totalPages ? totalPages- pagesSize * (currentPageIndex/pagesSize) : pagesSize;
    
                int pageIndex = pagesSize * (int)(currentPageIndex / pagesSize);
                for (int i = 0; i < pageIndexMaxIncrement; i++)
                {
                    if (pageIndex == currentPageIndex)
                    {
                        sb.Append(string.Format("<a href='{0}' style='{2}'>{1}</a>", "javascript:void(0)", (pageIndex + 1).ToString(), "text-decoration:none;"));
    
                    }
                    else
                    {
                        sb.Append(string.Format("<a href='{0}'>{1}</a>", requestBaseUrl + "/" + pageIndex.ToString(), (pageIndex + 1).ToString()));
    
                    }
                    sb.Append("&nbsp;&nbsp;");
                    pageIndex++;
                }
                href = "javascript:void(0)";
                if (pageIndex<totalPages-1)
                {
                    href = string.Format(requestBaseUrl + "/" + ((currentPageIndex / pagesSize+1) * pagesSize - 1).ToString());
                }
                sb.Append(string.Format("<a href=\"{0}\"><img src=\"{1}\" style=\"boder:0px;border-color: #FFFFFF\"/></a>", href, imageUrlForDown));
    
                return sb.ToString();
            }
            public static string CreatePager(this HtmlHelper helper,Pager pager)
            {
                if (string.IsNullOrEmpty(pager.ImageUrlForDown.ToString()) || string.IsNullOrEmpty(pager.ImageUrlForUp.ToString()))
                {
                    return CreatePager(pager.CurrentPageIndex, pager.TotalPages, pager.RequestBaseUrl);
                }
                else if (pager.PagesSize<=1)
                {
                    return CreatePager(pager.CurrentPageIndex, pager.TotalPages, pager.RequestBaseUrl, pager.ImageUrlForUp.ToString(), pager.ImageUrlForDown.ToString());
                }
                else
                {
                    return CreatePager(pager.CurrentPageIndex, pager.TotalPages, pager.RequestBaseUrl,pager.PagesSize, pager.ImageUrlForUp.ToString(), pager.ImageUrlForDown.ToString());
    
                }
            }
            public static void Validate(int currentPageIndex,int totalPages)
            {
                if (currentPageIndex<0)
                {
                    throw new Exception("The current page indx must not be less than zero");
                }
                if (totalPages < 1)
                {
                    throw new Exception("The total pages must not be less than 1");
                }
                if (currentPageIndex>totalPages-1)
                {
                    throw new Exception("The current page index can't be greater than total pages");
                }
            }
            public static void Validate(int currentPageIndex, int totalPages,int pagesSize)
            {
                Validate(currentPageIndex, totalPages);
                if (pagesSize<0)
                {
                    throw new Exception("The page size must be greater thatn zero");
                }
            }
        }


     public ActionResult ListNews(int? id)
            {
                int pageIndex=Convert.ToInt32(id);
                int pageSize = 2;
                List<News> allNews = rbe.NewsSet.ToList();
                List<News> newsList = allNews.OrderByDescending(n=>n.PublishDate).Skip(pageIndex * pageSize).Take(pageSize).ToList();
                int count=allNews.Count();
                int remainder=count%pageSize;
                int quotient = count / pageSize;
                Pager pager = new Pager(pageIndex, (remainder == 0 ? quotient : quotient + 1), "/Home/ListNews", "/images/left.gif", "/images/right.gif", 5);
                ViewData["pager"] = pager;
               
                return View(newsList);
            }

    Microsoft Online Community Support
    2010年3月15日 4:08
  • 你好,

    可以按照下面的方式设计:
    1.分类列表用来List Categories。
    2.点击分类列表中的连接导航到该category对应的数据页面,在这页面分页。
    3.分页代码可以参考如下:
    public class Pager
    
        {
    
            private int _currentPageIndex =-1;
    
            private int _totalPages = -1;
    
            private string _reqBaseUrl = string.Empty;
    
            private string _imgUrlForUp = string.Empty;
    
            private string _imgUrlForDown = string.Empty;
    
            private int _pagesSize=1;
    
            public Pager(int currentPageIndex, int totalPages, string requestBaseUrl)
    
            {
    
    
    
                _currentPageIndex = currentPageIndex;
    
                _totalPages = totalPages;
    
                _reqBaseUrl = requestBaseUrl;   
    
            }
    
            public Pager(int currentPageIndex, int totalPages, string requestBaseUrl, string imgUrlForUp, string imgUrlForDown)
    
            {
    
    
    
                _currentPageIndex = currentPageIndex;
    
                _totalPages = totalPages;
    
                _reqBaseUrl = requestBaseUrl;
    
                _imgUrlForDown = imgUrlForDown;
    
                _imgUrlForUp = imgUrlForUp;       
    
    
    
            }
    
            //public string CreatePager()
    
            //{
    
            //  return CustomizePager.CreatePager(this);
    
            //}
    
            public Pager(int currentPageIndex, int totalPages, string requestBaseUrl, string imgUrlForUp, string imgUrlForDown, int pagesSize)
    
            {
    
    
    
                _currentPageIndex = currentPageIndex;
    
                _totalPages = totalPages;
    
                _reqBaseUrl = requestBaseUrl;
    
                _imgUrlForDown = imgUrlForDown;
    
                _imgUrlForUp = imgUrlForUp;
    
                _pagesSize = _pagesSize > pagesSize ? _pagesSize : pagesSize;
    
    
    
            }
    
        
    
            public int CurrentPageIndex { get{return _currentPageIndex;} }
    
            public int TotalPages { get { return _totalPages; } }
    
            public string RequestBaseUrl { get { return _reqBaseUrl; } }
    
            public string ImageUrlForUp { get { return _imgUrlForUp; } }
    
            public string ImageUrlForDown { get { return _imgUrlForDown; } }
    
            public int PagesSize { get { return _pagesSize; } }
    
        }
    
        public static class CustomizePager
    
        {
    
    
    
            /// <summary>
    
            /// 
    
            /// </summary>
    
            /// <param name="currentPageIndex">current page index</param>
    
            /// <param name="totalPages">total pages</param>
    
            /// <param name="requestBaseUrl">base url and value construct a whole url, eg:http://baseURL/id</param>   
    
            public static string CreatePager(int currentPageIndex, int totalPages, string requestBaseUrl)
    
            {
    
                Validate(currentPageIndex, totalPages);
    
                StringBuilder sb = new StringBuilder();
    
                if (currentPageIndex > 0)
    
                {
    
                    sb.Append(string.Format("<a href=\"{0}\">Up</a>", requestBaseUrl + "/" + (currentPageIndex - 1).ToString()));
    
    
    
                }
    
                if (currentPageIndex != totalPages - 1)
    
                {
    
                    sb.Append(string.Format("<a href=\"{0}\">Down</a>", requestBaseUrl + "/" + (currentPageIndex + 1).ToString()));
    
                }
    
                return sb.ToString();
    
            }
    
    
    
            /// <summary>
    
            /// 
    
            /// </summary>
    
            /// <param name="currentPageIndex">current page index</param>
    
            /// <param name="totalPages">total pages</param>
    
            /// <param name="requestBaseUrl">base url and value construct a whole url, eg:http://baseURL/id</param>   
    
            /// <param name="imageUrlForUp">image for previous page</param>
    
            /// <param name="imageUrlForDown">image for next page</param>
    
            /// <returns></returns>
    
            public static string CreatePager(int currentPageIndex, int totalPages, string requestBaseUrl,string imageUrlForUp,string imageUrlForDown)
    
            {
    
                StringBuilder sb = new StringBuilder();
    
                Validate(currentPageIndex, totalPages);
    
               
    
                if (currentPageIndex > 0)
    
                {
    
                    sb.Append(string.Format("<a href=\"{0}\"><img src={1}/></a>", requestBaseUrl + "/" + (currentPageIndex - 1).ToString(),imageUrlForUp));
    
    
    
                }
    
                if (currentPageIndex != totalPages - 1)
    
                {
    
                    sb.Append(string.Format("<a href=\"{0}\"><img src={1}/></a>", requestBaseUrl + "/" + (currentPageIndex - 1).ToString(),imageUrlForDown));
    
    
    
                }
    
    
    
                return sb.ToString();
    
            }
    
            /// <summary>
    
            /// 
    
            /// </summary>
    
            /// <param name="currentPageIndex">current page index</param>
    
            /// <param name="totalPages">total pages</param>
    
            /// <param name="requestBaseUrl">base url and value construct a whole url, eg:http://baseURL/id</param>
    
            /// <param name="pagesSize">size of pagers which are displayed at one time </param>
    
            /// <param name="imageUrlForUp">image for previous page</param>
    
            /// <param name="imageUrlForDown">image for next page</param>
    
            /// <returns></returns>
    
            public static string CreatePager(int currentPageIndex, int totalPages, string requestBaseUrl,int pagesSize, string imageUrlForUp, string imageUrlForDown)
    
            {
    
                StringBuilder sb = new StringBuilder();
    
                Validate(currentPageIndex, totalPages,pagesSize);
    
                string href = "javascript:void(0)";
    
                if (currentPageIndex/pagesSize != 0)
    
                {
    
                    href = string.Format(requestBaseUrl+"/"+((currentPageIndex/pagesSize)*pagesSize-1).ToString());
    
                }
    
    
    
                sb.Append(string.Format("<a href=\"{0}\"><img src=\"{1}\"/></a>",href,imageUrlForUp));
    
                sb.Append("&nbsp;&nbsp;");
    
                
    
                int pageIndexMaxIncrement = (currentPageIndex / pagesSize + 1) * pagesSize > totalPages ? totalPages- pagesSize * (currentPageIndex/pagesSize) : pagesSize;
    
    
    
                int pageIndex = pagesSize * (int)(currentPageIndex / pagesSize);
    
                for (int i = 0; i < pageIndexMaxIncrement; i++)
    
                {
    
                    if (pageIndex == currentPageIndex)
    
                    {
    
                        sb.Append(string.Format("<a href='{0}'>{1}</a>", "javascript:void(0)", (pageIndex + 1).ToString()));
    
    
    
                    }
    
                    else
    
                    {
    
                        sb.Append(string.Format("<a href='{0}'>{1}</a>", requestBaseUrl + "/" + pageIndex.ToString(), (pageIndex + 1).ToString()));
    
    
    
                    }
    
                    sb.Append("&nbsp;&nbsp;");
    
                    pageIndex++;
    
                }
    
                href = string.Empty;
    
                if (pageIndex<totalPages-1)
    
                {
    
                    href = string.Format(requestBaseUrl + "/" + ((currentPageIndex / pagesSize+1) * pagesSize - 1).ToString());
    
                }
    
                sb.Append(string.Format("<a href=\"{0}\"><img src=\"{1}\"/></a>", href, imageUrlForDown));
    
    
    
                return sb.ToString();
    
            }
    
            public static string CreatePager(this HtmlHelper helper,Pager pager)
    
            {
    
                if (string.IsNullOrEmpty(pager.ImageUrlForDown.ToString()) || string.IsNullOrEmpty(pager.ImageUrlForUp.ToString()))
    
                {
    
                    return CreatePager(pager.CurrentPageIndex, pager.TotalPages, pager.RequestBaseUrl);
    
                }
    
                else if (pager.PagesSize > 1)
    
                {
    
                    return CreatePager(pager.CurrentPageIndex, pager.TotalPages, pager.RequestBaseUrl, pager.ImageUrlForUp.ToString(), pager.ImageUrlForDown.ToString());
    
                }
    
                else
    
                {
    
                    return CreatePager(pager.CurrentPageIndex, pager.TotalPages, pager.RequestBaseUrl,pager.PagesSize, pager.ImageUrlForUp.ToString(), pager.ImageUrlForDown.ToString());
    
    
    
                }
    
            }
    
            public static void Validate(int currentPageIndex,int totalPages)
    
            {
    
                if (currentPageIndex<0)
    
                {
    
                    throw new Exception("The current page indx must not be less than zero");
    
                }
    
                if (totalPages<1)
    
                {
    
                    throw new Exception("The total pages must not be less than one");
    
                }
    
                if (currentPageIndex>totalPages-1)
    
                {
    
                    throw new Exception("The current page index can't be greater than total pages");
    
                }
    
            }
    
            public static void Validate(int currentPageIndex, int totalPages,int pagesSize)
    
            {
    
                Validate(currentPageIndex, totalPages);
    
                if (pagesSize<0)
    
                {
    
                    throw new Exception("The page size must be greater thatn zero");
    
                }
    
            }
    
        }
    
    
      public ActionResult ListNews(int? id)
    
            {
    
                int pageIndex=Convert.ToInt32(id);
    
                int pageSize = 10;
    
                List<News> allNews = rbe.NewsSet.ToList();
    
                List<News> newsList = allNews.OrderByDescending(n=>n.PublishDate).Skip(pageIndex * pageSize).Take(pageSize).ToList();
    
                Pager pager = new Pager(pageIndex, allNews.Count() / pageSize + 1, "/Home/ListNews", "/images/left.gif", "/images/right.gif",5);
    
                ViewData["pager"] = pager;
    
    
    
                return View(newsList);
    
            }
    
    
    然后在页面上获得Pager对象,调用CreatePager(this HtmlHelper helper,Pager pager)输出分页html代码。
    Microsoft Online Community Support

    在一个页面中既有分类列表,又有选择某分类的新闻列表,而且还需要排序,上面的思路可以实现?
    2010年3月15日 5:27
  • 分页和类别导航都已实现,问题是一个页面中既有类别列表,又有类别下数据列表。
    在分页导航时要按照分类和页码来做。

    2010年3月15日 5:38
  • 你好,

    根据你的描述,大致理解如下:

    页面中有类别列表和相应的数据列表。显示哪个数据列表根据你选择哪个类别来确定。
    这个数据列表要有分页。

    这个也好办。
    我上面的代码仅仅是输出页码。
    你要把categoryid以及页面index作为参数传入action method就可以了,怎么把这两个参数传进去有两个方法。
    1.在global中添加一条路由规则以便你可以传两个参数。
    2.或者你用request.form["category"]来获得category的id之类的信息。
    至于怎么分页,如果你觉得分页很简单,可以用你自己的方法,如果有问题可以参考我的代码。

    Microsoft Online Community Support
    2010年3月15日 9:30