Пример #1
0
        public IActionResult Add(City city)
        {
            // I might put some validation here, but I am lazy

            // TODO 05: Check model state before updating. If there are errors, return the form to the user.
            if (!ModelState.IsValid)
            {
                return(View(city));
            }

            // Use the dao to add the city
            int newCityId = cityDAO.AddCity(city);

            // TODO 02a: Store a message in TempData to be shown to the user
            TempData["message"] = $"The city '{city.Name}' was added. The new city ID is {newCityId}.";

            // Redirect to a confirmation page
            //return RedirectToAction("AddConfirmation", new { id = newCityId });
            // TODO 01: Redirect from add to search, passing Country and District in
            CitySearchVM vm = new CitySearchVM()
            {
                CountryCode = city.CountryCode,
                District    = city.District
            };

            return(RedirectToAction("Search", vm));
        }
        public IActionResult Search(CitySearchVM vm)
        {
            if (vm.CountryCode == null && vm.District == null)
            {
                ViewData["Message"] = "Enter search criteria";
                vm.Cities           = new List <City>();
            }
            else
            {
                vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);
            }

            // TODO 01c: Populate the country list on the view model by calling GetCountrySelectList

            // Call GetCountries() on the countryDAO

            // TODO 01: Add a SelectList to the view-model for country codes.
            // TODO 01b: Add a method to Generate Select List from the Country DAO. (GetCountrySelectList)
            //          This will require us to ask for another "injected" DAO
            IList <Country> countries = countryDAO.GetCountries();

            vm.CountryList = new SelectList(countries, "Code", "Name");

            return(View(vm));
        }
Пример #3
0
        public void TestSearch()
        {
            // Arrange
            CityMockDAO    cityDAO    = new CityMockDAO();
            CountryMockDAO countryDAO = new CountryMockDAO();
            CityController controller = new CityController(cityDAO, countryDAO);

            // Act
            CitySearchVM vm = new CitySearchVM()
            {
                CountryCode = "CC1",
                District    = ""
            };
            IActionResult result = controller.Search(vm);

            // Assert
            ViewResult   vr       = result as ViewResult;
            CitySearchVM vmResult = vr.Model as CitySearchVM;

            Assert.IsNotNull(vmResult, "Search did not return a CitySearchVM");

            Assert.AreEqual(2, vmResult.Cities.Count);
            Assert.AreEqual("City1", vmResult.Cities[0].Name);
            Assert.AreEqual("City2", vmResult.Cities[1].Name);
        }
Пример #4
0
        // TODO 02: Write the SearchResults action (string countryCode, string district) to do the work of searching for a city.

        public IActionResult Search(CitySearchVM vm)
        {
            //CitySearchVM vm = new CitySearchVM();
            //vm.CountryCode = countryCode;
            //vm.District = district;

            vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);
            return(View(vm));
        }
Пример #5
0
        public IActionResult Search(CitySearchVM vm)
        {
            if (vm.CountryCode != null)
            {
                // Use the dao to get the cities that match the search
                vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);
            }

            // Pass the results into the SearchResults view for display
            return(View(vm));
        }
Пример #6
0
        public IActionResult Search(CitySearchVM vm)
        {
            if (vm.CountryCode != null)
            {
                // Use the dao to get the cities that match the search
                vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);
            }

            // Call the GetCountries private method, which Populates the country list on the view model
            vm.CountryList = GetCountries();

            // Pass the results into the SearchResults view for display
            return(View(vm));
        }
Пример #7
0
        public IActionResult Search(CitySearchVM vm)
        {
            vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);

            if (vm.CountryCode != null && vm.CountryCode.Length > 0)
            {
                ViewData["Message"] = $"{vm.Cities.Count} cities were found";
            }

            // Set the CountryList property on the VM for dropdown display
            IList <Country> countries = countryDAO.GetCountries();

            vm.CountryList = new SelectList(countries, "Code", "Name");

            return(View(vm));
        }
Пример #8
0
        public IActionResult Search(CitySearchVM vm)
        {
            // TODO 01: See View City\Search.cshtml and update labels

            vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);

            // TODO 05: Set the CountryList property on the VM for dropdown display
            IList <Country> countries = countryDAO.GetCountries();

            vm.CountryList = new SelectList(countries, "Code", "Name");


            //List<SelectListItem> selectListItems = new List<SelectListItem>();
            //selectListItems.Add(new SelectListItem("Saturn", "Saturn"));

            return(View(vm));
        }
        public IActionResult Search(CitySearchVM vm)
        {
            // TODO 01: NOTE we are setting a Message if there is no search criteria
            if (vm.CountryCode == null && vm.District == null)
            {
                ViewData["Message"] = "Enter search criteria";
                vm.Cities           = new List <City>();
            }
            else
            {
                vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);
            }

            // Populate the country list on the view model
            // Call GetCountries() on the countryDAO.
            IList <Country> countries = countryDAO.GetCountries();

            vm.CountryList = new SelectList(countries, "Code", "Name");

            return(View(vm));
        }
        public IActionResult Search(CitySearchVM vm)
        {
            if (vm.CountryCode == null && vm.District == null)
            {
                ViewData["Message"] = "Enter search criteria";
                vm.Cities = new List<City>();
            }
            else
            {
                vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);
            }

            // TODO 01c: Populate the country list on the view model by calling GetCountrySelectList
            // Call GetCountries() on the CountryDAO

            IList<Country> countries = countryDAO.GetCountries();

            vm.CountryList = new SelectList(countries, "Code", "Name");

            return View(vm);
        }
Пример #11
0
        public IActionResult Add(City city)
        {
            // Check model state before updating. If there are errors, return the form to the user.
            if (!ModelState.IsValid)
            {
                return(View(city));
            }

            int newCityId = cityDAO.AddCity(city);

            // Add a confirmation message to the user and then re-direct to the search page
            TempData["Message"] = $"City '{city.Name}' was added with id {newCityId}";

            // Redirect to the search page
            CitySearchVM vm = new CitySearchVM()
            {
                CountryCode = city.CountryCode,
                District    = city.District
            };

            return(RedirectToAction("Search", vm));
        }
Пример #12
0
        // TODO: Protect City/Delete with AuthorizeFilter so the user must be logged in (Admin)
        public IActionResult Delete(City city)
        {
            // Get the city
            city = cityDAO.GetCityById(city.CityId);
            if (city == null)
            {
                return(NotFound());
            }

            // Delete the city
            cityDAO.DeleteCity(city.CityId);

            // Redirect from delete to search, passing Country and District in
            CitySearchVM vm = new CitySearchVM(GetCurrentUser())
            {
                CountryCode = city.CountryCode,
                District    = city.District
            };

            // Store a message in TempData to be shown to the user
            TempData["message"] = $"The city '{city.Name}' was deleted.";
            return(RedirectToAction("Search", vm));
        }
Пример #13
0
        // TODO: Protect City/Add with AuthorizeFilter so the user must be logged in (Admin)
        public IActionResult Add(CityVM cityVM)
        {
            // Check model state before updating. If there are errors, return the form to the user.
            if (!ModelState.IsValid)
            {
                return(View(cityVM));
            }

            // Use the dao to add the city
            int newCityId = cityDAO.AddCity(cityVM.City);

            // Store a message in TempData to be shown to the user
            TempData["message"]     = $"The city '{cityVM.City.Name}' was added. The new city ID is {newCityId}.";
            TempData["HighlightId"] = newCityId;

            // Redirect from add to search, passing Country and District in
            CitySearchVM vm = new CitySearchVM(GetCurrentUser())
            {
                CountryCode = cityVM.City.CountryCode,
                District    = cityVM.City.District
            };

            return(RedirectToAction("Search", vm));
        }
Пример #14
0
 public IActionResult Search(CitySearchVM vm)
 {
     vm.Cities = cityDAO.GetCities(vm.CountryCode, vm.District);
     return(View(vm));
 }