Пример #1
0
        // GET: PropertyController
        public async Task <ActionResult> Index()
        {
            IEnumerable <Property> propertyList = await _propertyBusiness.GetAllProperties();

            List <PropertyViewModel> propertyViewModelList = PropertyMapper.ToPropertyViewModelList(propertyList);

            return(View(propertyViewModelList));
        }
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page)
        {
            ViewBag.CurrentSort         = sortOrder;
            ViewBag.PropertyIDSortParm  = String.IsNullOrEmpty(sortOrder) ? "propertyid_desc" : "";
            ViewBag.NameSortParm        = sortOrder == "name_asc" ? "name_desc" : "name_asc";
            ViewBag.AddressSortParm     = sortOrder == "address_asc" ? "address_desc" : "address_asc";
            ViewBag.EmailSortParm       = sortOrder == "email_asc" ? "email_desc" : "email_asc";
            ViewBag.DescriptionSortParm = sortOrder == "description_asc" ? "description_desc" : "description_asc";
            ViewBag.StatusSortParm      = sortOrder == "status_asc" ? "status_desc" : "status_asc";
            ViewBag.PhoneSortParm       = sortOrder == "phone_asc" ? "phone_desc" : "phone_asc";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;


            IEnumerable <Property>          propertyList          = _propertyBusiness.GetAllProperties();
            IEnumerable <PropertyViewModel> propertyViewModelList = PropertyMapper.ToPropertyViewModelList(propertyList);

            if (!String.IsNullOrEmpty(searchString))
            {
                propertyViewModelList = propertyViewModelList.Where(s => s.Name.Contains(searchString) ||
                                                                    s.Address.Contains(searchString) || s.Description.Contains(searchString) ||
                                                                    s.Email.Contains(searchString) || s.Status.ToString().Contains(searchString) ||
                                                                    s.Phone.Contains(searchString) || s.PropertyID.ToString().Contains(searchString));
            }

            switch (sortOrder)
            {
            case "propertyid_desc":
                propertyViewModelList = propertyViewModelList.OrderByDescending(s => s.PropertyID);
                break;

            case "name_desc":
                propertyViewModelList = propertyViewModelList.OrderByDescending(s => s.Name);
                break;

            case "name_asc":
                propertyViewModelList = propertyViewModelList.OrderBy(s => s.Name);
                break;

            case "address_desc":
                propertyViewModelList = propertyViewModelList.OrderByDescending(s => s.Address);
                break;

            case "address_asc":
                propertyViewModelList = propertyViewModelList.OrderBy(s => s.Address);
                break;

            case "email_desc":
                propertyViewModelList = propertyViewModelList.OrderByDescending(s => s.Email);
                break;

            case "email_asc":
                propertyViewModelList = propertyViewModelList.OrderBy(s => s.Email);
                break;

            case "description_desc":
                propertyViewModelList = propertyViewModelList.OrderByDescending(s => s.Description);
                break;

            case "description_asc":
                propertyViewModelList = propertyViewModelList.OrderBy(s => s.Description);
                break;

            case "status_desc":
                propertyViewModelList = propertyViewModelList.OrderByDescending(s => s.Status);
                break;

            case "status_asc":
                propertyViewModelList = propertyViewModelList.OrderBy(s => s.Status);
                break;

            case "phone_desc":
                propertyViewModelList = propertyViewModelList.OrderByDescending(s => s.Phone);
                break;

            case "phone_asc":
                propertyViewModelList = propertyViewModelList.OrderBy(s => s.Phone);
                break;

            default:
                propertyViewModelList = propertyViewModelList.OrderBy(s => s.PropertyID);
                break;
            }

            int pageSize   = 3;
            int pageNumber = (page ?? 1);

            return(View(propertyViewModelList.ToPagedList(pageNumber, pageSize)));
        }