<Script> function GetCityByParentID() { $("#City").empty(); $.ajax({ url: '/Doctor/ChangeCity/' + $("#State").val(), type: "post", datatype: "json", success: function (data) { if (data.length == 0) { $("<option></option>") .val("0") .text("请选择市区") .appendTo($("#City")); } $.each(data, function (i, item) { $("<option></option>") .val(item["Id"]) .text(item["Name"]) .appendTo($("#City")); }); GetHospitalByID(); } }); } function GetHospitalByID() { $("#Hospital").empty(); $.ajax({ url: '/Doctor/ChangeHospital/' + $("#City").val(), type: "post", datatype: "json", success: function (data) { if (data.length == 0) { $("<option></option>") .val("0") .text("请选择医院") .appendTo($("#Hospital")); } $.each(data, function (i, item) { $("<option></option>") .val(item["Id"]) .text(item["Name"]) .appendTo($("#Hospital")); }); } }); }
后台写法
private void BindState() { AreaInfoRepository areaBll = new AreaInfoRepository(); var stateList = areaBll.QueryParent(0); if (stateList == null) { stateList = new List<AreaInfoModel>(); } var select = new List<SelectListItem>(); foreach (var item in stateList) { select.Add(new SelectListItem { Text = item.Name, Value = item.Id.ToString() }); } select.Insert(0, new SelectListItem { Text = "请选择省", Value = "0" }); ViewBag.StateList = select; var city = new List<SelectListItem>(); city.Insert(0, new SelectListItem { Text = "请选择市区", Value = "0" }); ViewBag.CityList = city; } private void BindHospital() { HospitalRepository roleBll = new HospitalRepository(); var list = roleBll.Query(); if (list == null) { list = new List<HospitalModel>(); } var select = new List<SelectListItem>(); foreach (var item in list) { select.Add(new SelectListItem { Text = item.Name, Value = item.Id.ToString() }); } select.Insert(0, new SelectListItem { Value = "0", Text = "请选择医院" }); ViewBag.HospitalList = select; } public ActionResult ChangeCity(int id = 0) { AreaInfoRepository areaBll = new AreaInfoRepository(); var cityList = new List<AreaInfoModel>(); if (id != 0) { cityList = areaBll.QueryParent(id); if (cityList == null) { cityList = new List<AreaInfoModel>(); } } return Json(cityList, JsonRequestBehavior.AllowGet); } public ActionResult ChangeHospital(int id = 0) { HospitalRepository hospBLL = new HospitalRepository(); var hospList = new List<HospitalModel>(); if (id != 0) { hospList = hospBLL.QueryParent(id); if (hospList == null) { hospList = new List<HospitalModel>(); } } return Json(hospList, JsonRequestBehavior.AllowGet); } [HttpPost] public ActionResult Form(ExpertRegisterModel model) { BindNation(); BindJobTitle(); BindState(); model.Doctor.Id = 0; if (ModelState.IsValid) { var form = HttpContext.Request.Form; if (form.Count > 0) { var name = form["State"].ToString(); var hospital = form["Hospital"].ToString(); if (name != "") { model.Doctor.DominID = int.Parse(name); model.Doctor.Hospital = int.Parse(hospital); } model.Doctor.ChargeUnit = form["ChargeUnitList"].ToString(); } if (model.Doctor.Id == 0) { bll.AddExpand(model); } else { bll.UpdateExpand(model.Doctor.Id, model); } ViewBag.Msg = "编辑成功!"; ViewBag.ReturnUrl = "Form"; } var goOn = HttpContext.Request.Form["isGoOnAdd"]; if (goOn == null || goOn.ToString() == "false") { return RedirectToAction("List"); } else { return View(new ExpertRegisterModel()); } }