コード例 #1
0
        public ActionResult Edit(int? id)
        {
            CitiesService citiesService = new CitiesService();

            CitiesEditVM model = new CitiesEditVM();
            City city;

            if (!id.HasValue)
            {
                city = new City();
            }
            else
            {
                city = citiesService.GetByID(id.Value);
                if (city == null)
                {
                    return RedirectToAction("List");
                }
            }

            model.ID = city.ID;
            model.Name = city.Name;
            model.PostCode = city.PostCode;
            model.CountryID = city.CountryID;

            //model.Countries = new CountriesRepository().GetAll(); 1
            //model.Countries = new SelectList(new CountriesRepository().GetAll(), "ID", "Name"); //2
            model.Countries = citiesService.GetSelectedCountries(); // 3

            return View(model);
        }
コード例 #2
0
        public ActionResult Edit()
        {
            CitiesService citiesService = new CitiesService();

            CitiesEditVM model = new CitiesEditVM();
            TryUpdateModel(model);

            if (!ModelState.IsValid)
            {
                model.Countries = citiesService.GetSelectedCountries();
                return View(model);
            }

            City city;
            if (model.ID == 0)
            {
                city = new City();
            }
            else
            {
                city = citiesService.GetByID(model.ID);
                if (city == null)
                {
                    return RedirectToAction("List");
                }
            }

            city.ID = model.ID;
            city.Name = model.Name;
            city.PostCode = model.PostCode;
            city.CountryID = model.CountryID;

            citiesService.Save(city);

            return RedirectToAction("List");
        }