public ActionResult SearchProperty()
        {
            Stopwatch sw = Stopwatch.StartNew();

            try
            {
                selectedPriceRange   = Int32.Parse(Request.Form["priceRange_DDL"].ToString());
                selectedPropertyType = Int32.Parse(Request.Form["propertyType_DDL"].ToString());
                selectedRoomType     = Int32.Parse(Request.Form["roomType_DDL"].ToString());
                selectedDistrict     = Int32.Parse(Request.Form["district_DDL"].ToString());

                foreach (PremiseType type in premisesTypeList)
                {
                    type.isChecked = Convert.ToBoolean(Request.Form[type.premises_type_name].ToString().Split(',')[0]);
                }
            }
            catch
            {
                if (!validateForm(selectedPriceRange, selectedPropertyType, selectedRoomType, selectedDistrict))
                {
                    return(RedirectToAction("Index", new { redirect = true }));
                }
            }

            if (!validateForm(selectedPriceRange, selectedPropertyType, selectedRoomType, selectedDistrict))
            {
                return(RedirectToAction("Index", new { redirect = true }));
            }

            string priceRangeStr   = priceRangeList.Where(price => price.Value == selectedPriceRange.ToString()).First().Text;
            string propertyTypeStr = propertyTypeList.Where(type => type.Value == selectedPropertyType.ToString()).First().Text;

            propertyTypeStr = propertyTypeStr.ToLower();
            string roomTypeStr = roomTypeList.Where(type => type.Value == selectedRoomType.ToString()).First().Text;
            string districtStr = districtAreaList.Where(district => district.Value == selectedDistrict.ToString()).First().Text;


            Town town = townGateway.SelectByTownName(districtStr);

            if (town == null)
            {
                return(RedirectToAction("Index"));
            }

            int[] minMaxPrice = getMinMaxPrice(priceRangeStr);

            int minBuiltSize = Convert.ToInt16(Property.GetMinBuiltSize(roomTypeStr)) - 1; // Round down
            int maxBuiltSize = Convert.ToInt16(Property.GetMaxBuiltSize(roomTypeStr)) + 1; // Round up

            propertyController.clearListProperty();

            List <Property> allProperties = propertyGateway.GetProperties
                                                (town.town_id, minMaxPrice[0], minMaxPrice[1], minBuiltSize, maxBuiltSize, propertyTypeStr);


            List <int> type_id = new List <int>();

            for (int i = 0; i < premisesTypeList.Count; i++)
            {
                anyPremisesChecked |= premisesTypeList[i].isChecked;
                allPremisesChecked &= premisesTypeList[i].isChecked;
                if (premisesTypeList[i].isChecked)
                {
                    type_id.Add(premisesTypeList[i].premises_type_id);
                }
            }

            List <Premise> allPremises = new List <Premise>();

            try
            {
                if (!anyPremisesChecked || allPremisesChecked)
                {
                    foreach (Premise p in premisesList)
                    {
                        if (p.town_id == town.town_id)
                        {
                            allPremises.Add(p);
                        }
                    }
                    //allPremises = premisesGateway.SelectAll(town.town_id).ToList();
                }
                else
                {
                    foreach (Premise p in premisesList)
                    {
                        if (p.town_id == town.town_id)
                        {
                            foreach (int typeid in type_id)
                            {
                                if (typeid == p.premises_type_id)
                                {
                                    allPremises.Add(p);
                                    break;
                                }
                            }
                        }
                    }
                    //allPremises = premisesGateway.GetPremises(town.town_id, type_id.ToArray()).ToList();
                }
            }
            catch (Exception ex)
            {
                string mes = ex.Message;
            }

            foreach (Property p in allProperties)
            {
                Agent agt = agentGateway.SelectById(p.agent_id);
                PropertyWithPremises pwp = new PropertyWithPremises()
                {
                    property = p, agent = agt, listOfPremise = findPremises(p, allPremises)
                };                                                                                                                                 //new List<Premise>() };

                if (pwp.listOfPremise.Count > 0)
                {
                    propertyController.addProperty(pwp);
                }
            }

            Config(selectedPriceRange, selectedPropertyType, selectedRoomType, selectedDistrict);

            sw.Stop();
            Debug.WriteLine(allProperties.Count + ": " + sw.ElapsedMilliseconds + " ms");
            return(View("Index", propertyController.getAllProperties()));
        }
        public ActionResult SearchProperty()
        {
            try {
                selectedPriceRange   = Int32.Parse(Request.Form["priceRange_DDL"].ToString());
                selectedPropertyType = Int32.Parse(Request.Form["propertyType_DDL"].ToString());
                selectedRoomType     = Int32.Parse(Request.Form["roomType_DDL"].ToString());
                selectedDistrict     = Int32.Parse(Request.Form["district_DDL"].ToString());

                foreach (PremiseType type in premisesTypeList)
                {
                    type.isChecked = Convert.ToBoolean(Request.Form[type.premises_type_name].ToString().Split(',')[0]);
                }
            }
            catch
            {
                if (!validateForm(selectedPriceRange, selectedPropertyType, selectedRoomType, selectedDistrict))
                {
                    return(RedirectToAction("Index", new { redirect = true }));
                }
            }

            if (!validateForm(selectedPriceRange, selectedPropertyType, selectedRoomType, selectedDistrict))
            {
                return(RedirectToAction("Index", new { redirect = true }));
            }

            string priceRangeStr   = priceRangeList.Where(price => price.Value == selectedPriceRange.ToString()).First().Text;
            string propertyTypeStr = propertyTypeList.Where(type => type.Value == selectedPropertyType.ToString()).First().Text;

            propertyTypeStr = propertyTypeStr.ToLower();
            string roomTypeStr = roomTypeList.Where(type => type.Value == selectedRoomType.ToString()).First().Text;
            string districtStr = districtAreaList.Where(district => district.Value == selectedDistrict.ToString()).First().Text;

            Town town = townGateway.SelectByTownName(districtStr);

            if (town == null)
            {
                return(RedirectToAction("Index"));
            }

            int[] minMaxPrice = getMinMaxPrice(priceRangeStr);

            int minBuiltSize = Convert.ToInt16(Property.GetMinBuiltSize(roomTypeStr)) - 1; // Round down
            int maxBuiltSize = Convert.ToInt16(Property.GetMaxBuiltSize(roomTypeStr)) + 1; // Round up

            propertyController.clearListProperty();

            List <Property> allProperties = propertyGateway.GetProperties
                                                (town.town_id, minMaxPrice[0], minMaxPrice[1], minBuiltSize, maxBuiltSize, propertyTypeStr);

            foreach (Property p in allProperties)
            {
                Agent agt = agentGateway.SelectById(p.agent_id);
                PropertyWithPremises pwp = new PropertyWithPremises()
                {
                    property = p, agent = agt, listOfPremise = findPremises(p)
                };
                if (pwp.listOfPremise.Count > 0)
                {
                    propertyController.addProperty(pwp);
                }
            }

            Config(selectedPriceRange, selectedPropertyType, selectedRoomType, selectedDistrict);
            return(View("Index", propertyController.getAllProperties()));
        }