public ActionResult CreateOrUpdateState(StateProvinceModel model) { var country = countryService.GetById(model.CountryId); if (country == null) //No country found with the specified id return Json(new { IsValid = false, errorMessage = "Country does not exists" }); if (ModelState.IsValid) { if (model.RowId.IsEmpty()) stateProvinceService.Insert(model.ToEntity()); else { var stateProvince = stateProvinceService.GetById(model.RowId); if ( stateProvince == null ) //No state found with the specified id return Json(new { IsValid = false, errorMessage = "State does not exists" }); stateProvince = model.ToEntity(stateProvince); stateProvinceService.Update(stateProvince); } return Json( new {IsValid = true}); } //If we got this far, something failed, redisplay form return Json(new { IsValid = false, htmlData = RenderPartialViewToString("_CreateOrUpdateState", model) }); }
//create or update public ActionResult CreateOrUpdateState(Guid rowId, Guid countryId) { var model = new StateProvinceModel(); if (rowId.IsEmpty()) model.CountryId = countryId; else { var stateProvince = stateProvinceService.GetById(rowId); if ( stateProvince != null) { model = stateProvince.ToModel(); model.IsEdit = true; } } return PartialView("_CreateOrUpdateState", model); }