public ActionResult AddDistrict(District model) { var cities = db.Cities.OrderBy(c => c.CityName).ToList(); ViewBag.Cities = cities; if (ModelState.IsValid) { var district = new District(); district.DistrictName = model.DistrictName; district.CityID = model.CityID; db.Districts.Add(district); db.SaveChanges(); return Redirect("/Admin/Foundation/District"); } else { ModelState.AddModelError("", "区县信息失败"); } return View(); }
public ActionResult DistrictEdit(District model) { var district = db.Districts.Find(model.ID); List<SelectListItem> cityList = new List<SelectListItem>(); var cities = db.Cities.ToList(); foreach (var city in cities) { cityList.Add(new SelectListItem { Text = city.CityName, Value = city.ID.ToString(), Selected = (city.ID == district.CityID ? true : false) }); } ViewBag.CityList = cityList; ViewBag.District = district; if (ModelState.IsValid) { district.DistrictName = model.DistrictName; district.CityID = model.CityID; db.SaveChanges(); return Redirect("/Admin/Foundation/District"); } else { ModelState.AddModelError("", "区县信息失败"); } return View(); }