Saturday 16 August 2014

Jquery with MVC Fill Grid

//***************Controller Code*****************

 public class EmpController : Controller
    {
        //
        // GET: /Emp/
         static string orderBy = "asc";  // for sorting
        testingEntities db = null;
        public EmpController()
        {
            db = new testingEntities();
       
        }
               

        public ActionResult Index()
        {
            return View();
        }

         [OutputCache(Duration = 0)]
        public JsonResult GetEmployee(int pn,string col)
        {

         EmployeeGroupModel EmpGm=new EmployeeGroupModel();
         EmpGm.EmpList = db.Employees.OrderBy(em => em.EmpId)ToList();

             return Json(EmpGm,JsonRequestBehavior.AllowGet);
        }

    }

//********************Code in View*************************************

@{
    ViewBag.Title = "Index";
   
}

<!DOCTYPE html>

<html>
    <head>
        <script src="../../Scripts/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            GetData(1, 'EmpId');

        });

        function GetData(pn,col)
        {
            $.post('Emp/GetEmployee', {pn:pn,col:col}, function (data) { CheckData(data) });
        }

        function CheckData(abc)
        {
            //debugger;
            FillGrid(abc.EmpList, abc.NoOfPages);
        }

                 
       function FillGrid(abc,pn)
        {

         
            var tab = "<table>"
            tab += "<tr>"
            tab += "<td  style='background:silver; width:100px' >EmpId</td>"
            tab += "<td style='background-color:silver; width:100px' >EmpName</td>"
            tab += "<td style='background-color:silver; width:100px' >EmpSal</td>"
            tab += "<td style='background-color:silver; width:100px'>Empdep</td>"

            tab += "</tr>"

            for (var it in abc) {
                tab += "<tr>";
                tab += "<td style='width:100px;background-color:silver;'>" + abc[it].EmpId + "</td>";
                tab += "<td style='width:100px;background-color:silver;'>" + abc[it].EmpName + "</td>";
                tab += "<td style='width:100px;background-color:silver;'>" + abc[it].EmpSal + "</td>";
                tab += "<td style='width:100px;background-color:silver;'>" + abc[it].Dep + "</td>";
                tab += "</tr>";
            }

         
            tab += "</table>"

            $('#d1').html(tab);

        }

    </script>

</head>
    <body>
        <div id="d1">


        </div>

    </body>

</html>

//************************Group Model Added in Model class*********************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCWith Jquery
{
    public class EmployeeGroupModel
    {

        public List<Employee> EmpList { get; set; }
        public int NoOfPages { get; set; }

    }
}

//********** Global.asax**************************************

  public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

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

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

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            // Use LocalDB for Entity Framework by default
            Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }

knockout js array filter

knockout js array filter