Exemplo n.º 1
0
        public ActionResult FilterBy(string _name, int _page = 1)
        {
            using (EmployeeEntities context = new EmployeeEntities())
            {
                EmployeeModel            em   = new EmployeeModel(context);
                List <EmployeeViewModel> list = new List <EmployeeViewModel>();
                IEnumerable <Employee>   employees;
                if ((_name ?? "") == "")
                {
                    employees = em.GetAllItems().OrderBy(e => e.FirstName).ThenBy(e => e.LastName).Skip((_page - 1) * itemsPerPage).Take(itemsPerPage);
                    foreach (Employee emp in employees)
                    {
                        EmployeeViewModel ev = new EmployeeViewModel();
                        em.ConvertModelToViewModel(emp, ev);
                        list.Add(ev);
                    }
                    ViewBag.Name     = _name;
                    ViewBag.PageInfo = new PageInfo()
                    {
                        CurrPage = _page, ItemsPerPage = itemsPerPage, TotalItems = em.GetAllItems().Count()
                    };
                    return(PartialView(list));
                }
                else
                {
                    //Delay simulation
                    Thread.Sleep(2000);

                    Expression <Func <Employee, bool> > exp = Employee => Employee.FirstName.Contains(_name) ||
                                                              Employee.LastName.Contains(_name);
                    employees = em.FilterItems(exp).OrderBy(e => e.FirstName).ThenBy(e => e.LastName).Skip((_page - 1) * itemsPerPage).Take(itemsPerPage);
                    foreach (Employee emp in employees)
                    {
                        EmployeeViewModel ev = new EmployeeViewModel();
                        em.ConvertModelToViewModel(emp, ev);
                        if (ev.PictureUrl != null)
                        {
                            ev.PictureUrl = EmployeeModel.PhotoFileUrl + "/" + ev.PictureUrl;
                        }
                        list.Add(ev);
                    }
                    ViewBag.Name     = _name;
                    ViewBag.PageInfo = new PageInfo()
                    {
                        CurrPage = _page, ItemsPerPage = itemsPerPage, TotalItems = em.FilterItems(exp).Count()
                    };
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    string output           = js.Serialize(list);
                    return(Json(output));
                }
            }
        }