代码之家  ›  专栏  ›  技术社区  ›  Ahmed

ASP中嵌套的服务器端块。网络MVC?

  •  1
  • Ahmed  · 技术社区  · 16 年前


    IEnumerable<Product>

     <a href="/Products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])-1)%>/" 
               class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 1 ? "bgn disabled" : ""%>">                          
                    previous 10 products                
            </a>
    



    <a href="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 0 ? 
            "javascript:void[]" : 
            "/products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])+1)%>/" %>" 
            class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 0 ? 
            "bgn disabled" :
            ""%>">
    
        previous 10 products
        </a>
    


    3 回复  |  直到 16 年前
        1
  •  2
  •   Mehrdad Afshari    16 年前

    if

    <% if (Convert.ToInt32(ViewData["CurrentPage"]) <= 0) { %>
         Disabled template goes here...
    <% } else { %> 
         Link template goes here...
    <% } %>
    

    顺便说一句,如果你对一组页面这样做,你可以把它封装在 ViewUserControl 或一个 ViewMasterPage .

        2
  •  1
  •   eu-ge-ne    16 年前

    这是另一种解决方案。添加 <script runat="server">

    <script runat="server">
        protected string Prev10Url {
            get {
                return Convert.ToInt32(ViewData["CurrentPage"]) <= 0
                    ? "javascript:void[]"
                    : "/products/Page" + Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])+1);
            }
        }
    
        protected string Prev10Class {
            get {
                return Convert.ToInt32(ViewData["CurrentPage"]) <= 0
                    ? "bgn disabled"
                    : "";
            }
        }
    
        protected string Next10Url {
            get {
                ...
            }
        }
    
        protected string Next10Class {
            get {
                ...
            }
        }
    </script>
    

    <a href="<%= Prev10Url %>" class="<%= Prev10Class %>">previous 10 products</a>
    <a href="<%= Next10Url %>" class="<%= Next10Class %>">next 10 products</a>
    
        3
  •  0
  •   Glorfindel Doug L.    6 年前

    你可以试试我的分页HTML助手:

    using System;
    using System.Text;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace System.Web.Mvc
    {
       public class Pager
       {
          private ViewContext viewContext;
          private readonly int pageSize;
          private readonly int currentPage;
          private readonly int totalItemCount;
          private readonly RouteValueDictionary linkWithoutPageValuesDictionary;
    
          public Pager(ViewContext viewContext, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary)
          {
             this.viewContext = viewContext;
             this.pageSize = pageSize;
             this.currentPage = currentPage;
             this.totalItemCount = totalItemCount;
             this.linkWithoutPageValuesDictionary = valuesDictionary;
          }
    
          public string RenderHtml()
          {
             int pageCount = (int)Math.Ceiling(this.totalItemCount / (double)this.pageSize);
             int nrOfPagesToDisplay = 8;
    
             var sb = new StringBuilder();
             sb.Append("<ul class=\"pagination\">");
             // Previous
             if (this.currentPage > 1)
             {
                sb.Append(string.Format("<li class=\"prev\"><a href=\"{0}\">«</a></li>", Route(this.currentPage - 1)));
             }
             else
             {
                sb.Append("<li class=\"prev disabled\"><span>«</span></li>");
             }
    
             int start = 1;
             int end = pageCount;
    
             if (pageCount > nrOfPagesToDisplay)
             {
                int middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1;
                int below = (this.currentPage - middle);
                int above = (this.currentPage + middle);
    
                if (below < 4)
                {
                   above = nrOfPagesToDisplay;
                   below = 1;
                }
                else if (above > (pageCount - 4))
                {
                   above = pageCount;
                   below = (pageCount - nrOfPagesToDisplay);
                }
    
                start = below;
                end = above;
             }
    
             if (start > 3)
             {
                sb.Append(GeneratePageLink("1", 1));
                sb.Append(GeneratePageLink("2", 2));
                    sb.Append("<li class=\"more\">...</li>");
             }
             for (int i = start; i <= end; i++)
             {
                if (i == this.currentPage)
                {
                   sb.Append(string.Format("<li class=\"page selected\"><span>{1}</span></li>", Route(i),i));
                }
                else
                {
                   sb.Append(GeneratePageLink(i.ToString(), i));
                }
             }
             if (end < (pageCount - 3))
             {
                    sb.Append("<li class=\"more\">...</li>");
                sb.Append(GeneratePageLink((pageCount - 1).ToString(), pageCount - 1));
                sb.Append(GeneratePageLink(pageCount.ToString(), pageCount));
             }
    
             // Next
             if (this.currentPage < pageCount)
             {
                sb.Append(string.Format("<li class=\"next\"><a href=\"{0}\">»</a></li>", Route(this.currentPage + 1)));
             }
             else
             {
                sb.Append("<li class=\"next disabled\"><span>»</span></li>");
             }
             sb.Append("</ul>");
             return sb.ToString();
          }
          private string Route(int pageNumber)
          {
    
             var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary);
             pageLinkValueDictionary.Add("page", pageNumber);
             var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary);
             return virtualPathData.VirtualPath;
    
          }
          private string GeneratePageLink(string linkText, int pageNumber)
          {
             var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary);
             pageLinkValueDictionary.Add("page", pageNumber);
             var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary);
    
             if (virtualPathData != null)
             {
    
                string linkFormat = "<li class=\"page\"><a href=\"{0}\">{1}</a></li>";
                return String.Format(linkFormat, virtualPathData.VirtualPath, linkText);
             }
             else
             {
                return null;
             }
          }
       }
    }
    

    <%= Html.Pager(10, (Request["page"].IsNotNull() ? Request["page"].ToInt() : 1), ViewData["Total"].ToInt(), new { category = Request["category"], alphabet = Request["alphabet"] })%>
    

    public ActionResult Index(string page, string category, string alphabet)
    {
       .....
       ViewData["Total"] = model.Count();
       return View(model.ToPagedList((page.IsNotNull() ? page.ToInt() - 1 : 0), 10));
    }
    

    最后输出:

    pager
    (来源: clip2net.com )

    推荐文章