Friday, 8 November 2013

Paging and Url Routing in mvc 3.0

First create controller Emp and do the code .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvcpagingmukesh.EF;

namespace Mvcpagingmukesh.Controllers
{
    public class EmpController : Controller
    {
        //
        // GET: /Emp/
        test_dbEntities db = new test_dbEntities();

        public ActionResult Index()
        {
         
            int pageSize = 3;
            int pageNo = 1;
            int totalPage = db.emps.Count() / pageSize;
            ViewData["tp"] = totalPage;

            int skp = (pageNo - 1) * pageSize;

            List<emp> Emp = db.emps.OrderBy(em => em.id)
                .Skip(skp).Take(pageSize).ToList();
            return View(Emp);
       
        }
        public ActionResult paging(int pageno)
        {
            int pageSize = 3;
            int pageNo = pageno;
            int totalPage = db.emps.Count() / pageSize;
            ViewData["tp"] = totalPage;


            int skp = (pageNo - 1) * pageSize;

            List<emp> Emp = db.emps.OrderBy(em => em.id)
                .Skip(skp).Take(pageSize).ToList();
            return View("Index", Emp);
        }
        public ActionResult Home()
        {
            return View();
        }

    }
}

----------------------------------------------------------------------------------------
and add view index


@model IEnumerable< Mvcpagingmukesh.EF.emp>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        <table border="2" >
        <tr>
        <td style="width:100px;">Emp Id</td>
        <td style="width:100px;">Emp Name</td>
        <td style="width:100px;">Emp age</td>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
        <td style="width:100px;">@item.id</td>
        <td style="width:100px;">@item.name</td>
        <td style="width:100px;">@item.age</td>
        </tr>
        }

        <tr>
        <td style="width:100px;" colspan="3" >
        @for (int i = 1; i <= (int)ViewData["tp"]; i++)  // this is used for paging hyperlink containg 
        {                                                                            // action name paging which would be call by                                                                                           //pageno
            @Html.ActionLink(i.ToString()+"  ", "paging",
                 new { pageno = i });
        }
        </td>       
        </tr>
        </table>

        @Html.ActionLink("Home Page","Home")   // to redirect to home page
    </div>
</body>
</html>
--------------------------------------------------------------------------------------------------
and also take view for home 


output would be like this:
---------------------------------------------
Emp Id Emp Name Emp age
23 deepak 45
24 hari 35
32 abc 23
1 2 3 4
Home Page

------------------------------------------------------------------
on click on home page it will redirect to home 

and output would be like this:

Home



Url Routing For this we need to do code on global.aspx
like this:  

if we click on paging then url would be like this 

=======>http://localhost:50054/p/3

if we click on home page then url would be like this 
======>http://localhost:50054/vipin.aspx



 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
            "Home", // Route name
            "vipin.aspx/{id}", // URL with parameters
            new { controller = "Emp", action = "Home", id = UrlParameter.Optional } // Parameter defaults
        );
       routes.MapRoute(
  "DefaulPt", // Route name
  "p/{pageno}", // URL with parameters
  new { controller = "Emp", action = "paging", pageno = UrlParameter.Optional } // Parameter defaults
);

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Emp", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }


No comments:

Post a Comment