Asp.net MVC學習日記七(實現分頁和排序)

來源:互聯網
上載者:User

在開始之前,您需要一個叫NBuilder的DLL,這個東西可以免除你要先在資料庫裡面造資料,http://code.google.com/p/nbuilder/downloads/detail?

name=NBuilder-3.0.1.zip

下面開始我們的工程:

1、在Global.asax下加上

  routes.MapRoute("Products", "{controller}/{action}/Page/{page}/Property/{property}/Reverse/{reverse}",
               new { controller = "Home", action = "Index", page = 1,property = "Name", reverse = false });

2、準備Product類

    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public string Description { get; set; }
        public DateTime CreateDate { get; set; }
    }

3、建立類ProductRepository,添加NBuilder的引用

    public class ProductRepository
    {
        public List<Product> GetProducts(int page, int recordCount,string sortField, bool reverseSort)
        {
            List<Product> result = Builder<Product>
                     .CreateListOfSize(page * recordCount).WhereAll()
                     .Have(x => x.Description = @"...").Build()
                     .SortBy(sortField)
                     .ToList();
            //reverse the sort?
            if (reverseSort)
                result.Reverse();
            //do the paging
            return result.Skip((page - 1) * recordCount).ToList();
        }
    }

4、建立類RepositoryHelperscs,擴充IList的SortBy方法(這個對於linq很有用喲)

   public static class RepositoryHelperscs
    {
        private static object GetPropertyValue(object obj,string property)
        {
            System.Reflection.PropertyInfo propertyInfo =
            obj.GetType().GetProperty(property);
            return propertyInfo.GetValue(obj, null);
        }

        public static IList<T> SortBy<T>(this IList<T> list,string property)
        {
            //order the results
            if (!String.IsNullOrEmpty(property))
                list = (from item in list.AsEnumerable()
                        orderby GetPropertyValue(item, property)
                        select item).ToList();
            return list;
        }
    }

5、在Index視圖中加上如下代碼

<% List<Product> products = ViewData["Products"] as List<Product>;%>
    <%int page = (int) ViewData["page"];%>
    <%string property = ViewData["property"].ToString();%>
    <% bool reverse = (bool) ViewData["reverse"];%>

    <% int PreviousPage = (page - 1) < 1 ? 1 : page - 1;%>
    <% int NextPage = page + 1;%>
    <%= Html.ActionLink("Previous", "Index", new { @page = PreviousPage, @property = property, @reverse = reverse })%> -
    <%= Html.ActionLink("Next", "Index", new { @page = NextPage,@property = property, @reverse = reverse })%>

    <%= Html.ActionLink("Sort By Name", "Index", new { @page = page, @property = "Name", @reverse = property == "Name" && reverse ? false : true

})%> |
    <%= Html.ActionLink("Sort By Price", "Index", new { @page = page, @property = "Price", @reverse = property == "Price" && reverse ? false : true

})%> |
    <%= Html.ActionLink("Sort By Date", "Index", new { @page = page, @property = "CreateDate", @reverse = property == "CreateDate" && reverse ?

false : true })%>
    <table>
        <tr>
            <th>
                ProductId
            </th>
            <th>
                Name
            </th>
            <th>
                Price
            </th>
            <th>
                Description
            </th>
            <th>
                CreateDate
            </th>
        </tr>
        <% foreach (Product p in products)
           { %>
        <tr>
            <td>
                <%= p.ProductId %>
            </td>
            <td>
                <%= p.Name %>
            </td>
            <td>
                <%= p.Price %>
            </td>
            <td>
                <%= p.Description %>
            </td>
            <td>
                <%= p.CreateDate %>
            </td>
        </tr>
        <% } %>
    </table>

你訪問http://localhost:4411/Home/Index/Page/1/Property/name/Reverse/看看,是不是你想要的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.