Exemplo n.º 1
0
        public async Task <ViewResult> Contacts(string sortOrder, string searchString)
        {
            var response = await _service.GetContactsAsync();

            var contacts = response.Contacts;

            contacts = SortOrFilterContacts(ViewBag, sortOrder, searchString, contacts);

            return(View(contacts));
        }
Exemplo n.º 2
0
        public async Task <ViewResult> GetContactsAsync(string sortOrder, string searchString)
        {
            ViewBag.NameSortParm    = string.IsNullOrEmpty(sortOrder) ? "name_desc" : "";;
            ViewBag.PhoneSortParm   = sortOrder == "phone" ? "phone_desc" : "phone";
            ViewBag.AddressSortParm = sortOrder == "address" ? "address_desc" : "address";

            var response = await _service.GetContactsAsync();

            var contacts = response.Contacts;

            if (!string.IsNullOrEmpty(searchString))
            {
                contacts = contacts.Where(s => s.Name.ToLower().Contains(searchString.ToLower())).ToList();
            }

            switch (sortOrder)
            {
            case "name_desc":
                contacts = contacts.OrderByDescending(s => s.Name.Split(' ')[1]).ToList();
                break;

            case "address":
                contacts = contacts.OrderBy(s => s.Address).ToList();
                break;

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

            case "phone":
                contacts = contacts.OrderBy(s => s.PhoneNumber).ToList();
                break;

            case "phone_desc":
                contacts = contacts.OrderByDescending(s => s.PhoneNumber).ToList();
                break;

            default:
                contacts = contacts.OrderBy(s => s.Name.Split(' ')[1]).ToList();
                break;
            }

            return(View(contacts));
        }