Sunday, 2 March 2014

grid in mvc with paging

Emp Id Emp Name Emp age
23 deepak trivedi 456531
34 vijay chauhan 23
35 sonu trivedi 67
1 2
Home Page

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);
          
        }
        [HttpPost]
        public ActionResult Index(emp em)
        {
            string name = em.name;
            string age =Convert.ToString(em.id);
            AccessMethod ac = new AccessMethod();
           int k= ac.insertRecord(name, age);

           if (k == 1)
           {
               ViewBag.insertsuccess = "Record insert successfully";
           }
            return View();

        }
        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();
        }

    }
}
-------------------------------------------------------------------------------------------
@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++)
        {
            @Html.ActionLink(i.ToString()+"  ", "paging",
                 new { pageno = i });
        }
        </td>       
        </tr>
        </table>

        @Html.ActionLink("Home Page","Home")
    </div>
</body>
</html>