asp.net mvc tricks 3, Pager

Source: Internet
Author: User

After the last article, was rated no technical content, but I think this is not a problem, because the article does not necessarily make all the people benefit, the article or suitable for beginners, or suitable for proficient. But in general, I think it is worthwhile to write an article that inspires people.

And I think this series is mainly in the development of the problems encountered and solutions, it is easy to think of, and after several development iterations to form. Or that sentence, these may not be some efficient skills, but it is really able to solve the problem, there may not be any advanced principles, but I think it is worth sharing.

3.Pager

3.1 Requirements and analog code

requirements, assuming that we have a list that has paging functionality, we may need a list of page numbers, such as

Let's simulate the action:

1: public ActionResult Index(int? p)
  2: {
  3:   if (!p.HasValue) p = 1;//如果未对p 传值就是第1页
  4:   var list = new List<int>();//生成一个模拟列表
  5:   for (var i = 0;  i < 10;i++ )
  6:   {
  7:     list.Add(p.Value);//是第几页就向中填充几个这个页码的数
  8:    }
  9:   return View(list);//强型传递给View
 10: }

In view I write the following display mode:

1: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  Inherits="System.Web.Mvc.ViewPage<List<int>>" %>
  2:
  3: <asp:Content  ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
  4:   Pager  for List
  5: </asp:Content>
  6:
  7: <asp:Content ID="indexContent"  ContentPlaceHolderID="MainContent" runat="server">
  8:   <div>
  9:      <ul>
 10:       <%foreach (int i in Model){//显示这个列表%>
 11:        <li><%=i %></li>
 12:       <%} %>
 13:     </ul>
 14:    </div>
 15:   <!--将在这里显示分页的部分-->
 16:  </asp:Content>

The following is the result of the run:

The first page URL is similar to/home/index?p=1

The second page URL is similar to/home/index?p=2

Other pages, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.