A simple Page Number System for ASP.NEt C# / MVC


So here we introduce a whole new simple system of making  a page number system to ASP.NET c# MVC. Page Number concept is used to show a fixed size results on multiple pages. It allows us to Split the larger database in the smaller systems and thus even reduce the load to the particular page.

Concept used here for paging system is very simple we will use two major variable namely startIndex and PageSzie. Start-index will be determine the the starting point of the database will vary from page to page on the other hand Pagesize will determine the number of results to be picked form the database sources .

Well, what is the data source? Your action could take a few defaulted arguments, i.e.

ActionResult Search(string query, int startIndex, int pageSize) {...}

defaulted in the routes setup so that startIndex is 0 and pageSize is (say) 20:(You can skip this)

        routes.MapRoute("Search", "Search/{query}/{startIndex}",
                        new
                        {
                            controller = "Home", action = "Search",
                            startIndex = 0, pageSize = 20
                        });

To split the feed, you can use LINQ quite easily:

var page = source.Skip(startIndex).Take(pageSize);

(or do a multiplication if you use "pageNumber" rather than "startIndex")

With LINQ-toSQL, EF, etc - this should "compose" down to the database, too.

You should then be able to use action-links to the next page (etc):

<%=Html.ActionLink("next page", "Search", new {
                query, startIndex = startIndex + pageSize, pageSize }) %>
or for the razor syntax

@Html.ActionLink("next page", "Search", new {
                query, startIndex = startIndex + pageSize, pageSize })
so this system can help you to create a simple paging system.
thanks g.

Share this:

ABOUT THE AUTHOR

Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible

0 comments:

Post a Comment