示例#1
0
        public ViewResult FilterList(EnterpriseListViewModel model, int page = 1)
        {
            // hack againts bug "Filter info is lost when we switch pages"
            // don't know how to pass here actual model, so we will use previous one
            if (model == null)
            {
                var cachedModel = Session["FilterModel"];
                if (cachedModel == null)
                {
                    return(View("Index", Index(null, page).Model));
                }
                else
                {
                    model = (EnterpriseListViewModel)cachedModel;
                }
            }

            if (ModelState.IsValid)
            {
                IList <EnterpriceType> types = new List <EnterpriceType>();
                foreach (var checkBox in model.TypeCategories)
                {
                    if (checkBox.Selected)
                    {
                        types.Add(this.GetEntType(checkBox.Value));
                    }
                }

                IList <int> ratings = new List <int>();
                foreach (var checkBox in model.RatingCategories)
                {
                    if (checkBox.Selected)
                    {
                        ratings.Add(int.Parse(checkBox.Value));
                    }
                }

                string name = model.SearchString;
                IEnumerable <Enterprise> query = ratings.Count == 0 && types.Count == 0 ?
                                                 entRepository.GetByName(name) : entRepository.GetFiltratedByName(ratings, types, name);
                if (model.SelectedSortingCategory == "Name")
                {
                    query = query.OrderBy(ent => ent.Name);
                }
                else
                {
                    query = query.OrderBy(ent => ent.Rating);
                }

                model.Enterprises = new List <Enterprise>(query);
            }

            EnterpriseListViewModel newModel = UpdateModel(model, page);

            model.PagingInfo       = new PagingInfo(newModel.PagingInfo);
            Session["FilterModel"] = new EnterpriseListViewModel(model);

            return(View("Index", newModel));
        }