Exemplo n.º 1
0
        public EventListViewModel GetEventList(SearchEventsModel searchModel, int page = 1)
        {
            int pageSize = 20;

            if (page < 1)
            {
                page = 1;
            }

            IEnumerable <Event> records = db.Events.ToList();

            // Remove any default information
            //searchModel.ParseRouteInfo();

            if (!String.IsNullOrEmpty(searchModel.Name))
            {
                string name = searchModel.Name.ToLower();
                records = records.Where(x => x.Name.ToLower().Contains(name));
            }

            if (searchModel.StartDate.HasValue)
            {
                records = records.Where(x => x.EventDate >= searchModel.StartDate.Value);
            }

            if (searchModel.EndDate.HasValue)
            {
                records = records.Where(x => x.EventDate <= searchModel.EndDate.Value);
            }

            if (!String.IsNullOrEmpty(searchModel.Description))
            {
                records = records.Where(x => x.Description.ToLower().Contains(searchModel.Description.ToLower()));
            }

            return(new EventListViewModel
            {
                Events = records
                         .OrderByDescending(o => o.EventDate)
                         .ThenBy(o => o.EventId)
                         .Skip((page - 1) * pageSize)
                         .Take(pageSize),
                SearchModel = searchModel,
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    PageSize = pageSize,
                    TotalItems = records.Count()
                }
            });
        }
        public ActionResult Index(SearchEventsModel search, int page = 1)
        {
            // Return all Events
            // If not a post-back (i.e. initial load) set the searchModel to session
            if (Request.Form.Count <= 0)
            {
                if (search.IsEmpty() && Session["SearchEventsModel"] != null)
                {
                    search = (SearchEventsModel)Session["SearchEventsModel"];
                }
            }

            var helper = new EventHelper();
            var model  = helper.GetEventList(search, search.ParsePage(page));

            Session["SearchEventsModel"] = search;

            //(search);

            return(View(model));
        }