public ActionResult NewCity() { string strErrText; //生成国家下拉列表项 DDSystem dd = new DDSystem(); List<Country> listCountry = dd.LoadCountrys(LoginAccountId, LoginStaffName, out strErrText); if (listCountry == null) { throw new Exception(strErrText); } List<SelectListItem> selectListCountry = new List<SelectListItem>(); selectListCountry.Add(new SelectListItem { Text = string.Empty, Value = string.Empty }); selectListCountry.AddRange(from c in listCountry select new SelectListItem { Text = c.Name, Value = c.Name }); ViewData["Countries"] = new SelectList(selectListCountry, "Value", "Text"); //生成空的省份下拉列表项 List<Province> listState = new List<Province>(); List<SelectListItem> selectListState = new List<SelectListItem>(); selectListState.Add(new SelectListItem { Text = string.Empty, Value = string.Empty }); selectListState.AddRange(from s in listState select new SelectListItem { Text = s.Name, Value = s.Name }); ViewData["States"] = new SelectList(selectListState, "Value", "Text"); //创建空的Model CityViewModel model = new CityViewModel(); return View(model); }
public ActionResult NewCity(CityViewModel model) { if (ModelState.IsValid) { //创建数据 City data = new City(); data.Name = model.Name; data.CountryName = model.CountryName; data.ProvinceName = model.ProvinceName; data.Remark = model.Remark ?? string.Empty; //保存数据 string strErrText; DDSystem city = new DDSystem(); if (city.InsertCity(data, LoginAccountId, LoginStaffName, out strErrText)) { return Json(string.Empty); } else { return Json(strErrText); } } return View(model); }
public ActionResult ModifyCity(string id) { string strErrText; //生成Model数据 DDSystem city = new DDSystem(); City data = city.LoadCity(long.Parse(id), LoginAccountId, LoginStaffName, out strErrText); if (data == null) { throw new Exception(strErrText); } CityViewModel model = new CityViewModel(); model.Id = data.Id; model.Name = data.Name; model.CountryName = data.CountryName; model.ProvinceName = data.ProvinceName; model.Remark = data.Remark; //生成国家下拉列表项 DDSystem dd = new DDSystem(); List<Country> listCountry = dd.LoadCountrys(LoginAccountId, LoginStaffName, out strErrText); if (listCountry == null) { throw new Exception(strErrText); } List<SelectListItem> selectListCountry = new List<SelectListItem>(); selectListCountry.Add(new SelectListItem { Text = string.Empty, Value = string.Empty }); selectListCountry.AddRange(from c in listCountry select new SelectListItem { Text = c.Name, Value = c.Name }); ViewData["Countries"] = new SelectList(selectListCountry, "Value", "Text", model.CountryName); //生成空的省份下拉列表项 List<Province> listState = null; if (!string.IsNullOrEmpty(model.CountryName)) { listState = dd.LoadProvincesByCountry(model.CountryName, LoginAccountId, LoginStaffName, out strErrText); if (listState == null) { throw new Exception(strErrText); } } else { listState = new List<Province>(); } List<SelectListItem> selectListState = new List<SelectListItem>(); selectListState.Add(new SelectListItem { Text = string.Empty, Value = string.Empty }); selectListState.AddRange(from s in listState select new SelectListItem { Text = s.Name, Value = s.Name }); ViewData["States"] = new SelectList(selectListState, "Value", "Text", model.ProvinceName); return View(model); }