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.