Пример #1
0
        public ActionResult SearchResult(FormCollection data, string pageNumber, string order)
        {
            var listCriteria      = db.Criteria.ToList();
            var listPosts         = new List <PostViewModel>();
            var listResult        = new List <PostViewModel>();
            var listSearchAdvange = new List <PostViewModel>();

            if (data == null)
            {
                return(View(listResult));
            }
            else
            {
                Session["searchData"] = data;
            }

            int    provinceID      = Convert.ToInt32(data["province"]);
            int    districtID      = Convert.ToInt32(data["district"]);
            string isSearchAdvance = data["isSearchAdvance"];

            Response.Cookies["provinceId"].Value = provinceID.ToString();
            Response.Cookies["townshipId"].Value = districtID.ToString();

            if (provinceID == 0 || districtID == 0)
            {
                ViewBag.province = "Province";
                ViewBag.district = "District";
            }
            else
            {
                ViewBag.province = db.Provinces.Where(p => p.ProvinceID == provinceID).First().ProvinceName;
                ViewBag.district = db.Districts.Where(p => p.DistrictID == districtID).First().DistrictName;
            }
            // Get all post
            listPosts  = PostViewModel.CreateListPostViewModel(provinceID, districtID);
            listResult = listPosts.Where(p => p.Post.PostStatus == true && p.Post.AccountStatus == 1).ToList();
            if (isSearchAdvance != null)
            {
                long price          = long.Parse(data["price"]);
                long area           = long.Parse(data["area"]);
                var  listCriteriaID = new List <int>();
                listPosts  = listResult.Where(p => p.Post.Price <= price && p.Post.Acreage <= area).ToList();
                listResult = listPosts;
                foreach (var item in listCriteria)
                {
                    if (data["criteria" + item.CriteriaID] != null)
                    {
                        var criID = Int32.Parse(data["criteria" + item.CriteriaID]);
                        listCriteriaID.Add(criID);
                    }
                }
                foreach (var item in listResult)
                {
                    var list = listCriteriaID.Except(item.Post.ListCriteriaID);
                    if (list.Count() == 0)
                    {
                        listSearchAdvange.Add(item);
                    }
                }
                listResult = listSearchAdvange;
            }

            var subTotalPage = listResult.Count / PageSize;
            var totalPage    = (listResult.Count % PageSize == 0) ? subTotalPage : subTotalPage + 1;

            var pageN = 1;

            if (!Int32.TryParse(pageNumber, out pageN))
            {
                pageN = 1;
            }
            else if ((pageN < 1) || (pageN > totalPage))
            {
                pageN = 1;
            }

            ViewBag.PageName   = "SearchResult";
            ViewBag.PageSize   = PageSize;
            ViewBag.PageNumber = pageN;
            ViewBag.TotalPage  = totalPage;
            if (order != null)
            {
                ViewBag.Order = order;
                if (order.Equals("asc"))
                {
                    var q = from s in listResult
                            orderby s.Post.Price ascending
                            select s;
                    return(View(q.ToList().Skip((pageN - 1) * PageSize).Take(PageSize)));
                }
                else if (order.Equals("desc"))
                {
                    var q = from s in listResult
                            orderby s.Post.Price descending
                            select s;
                    return(View(q.ToList().Skip((pageN - 1) * PageSize).Take(PageSize)));
                }
                else
                {
                    return(View(listResult.Skip((pageN - 1) * PageSize).Take(PageSize)));
                }
            }
            else
            {
                return(View(listResult.Skip((pageN - 1) * PageSize).Take(PageSize)));
            }
        }