public ActionResult AddCity(string nama, string lstprovince) { Random random = new Random(); const string chars = "0123456789"; string kode = new string(Enumerable.Repeat(chars, 7).Select(s => s[random.Next(s.Length)]).ToArray()); string userID = HttpContext.Session.GetString(SessionKeyID); AddCityInputModel data = new AddCityInputModel(); data.UserID = Guid.Parse(userID); data.Kode = kode; data.CityName = nama; data.KodeProvinsi = lstprovince.Split("|")[0].ToString(); JsonConvert.SerializeObject(data); using (var client = new HttpClient()) { client.BaseAddress = new Uri(BaseAPI + "Base/"); //HTTP POST var postTask = client.PostAsJsonAsync <AddCityInputModel>("AddCity", data); postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { return(RedirectToAction("Index", "City")); } else { TempData["CustomError"] = "Gagal menambah data. Mohon hubungi admin."; } } TempData["CustomError"] = "Terjadi kesalahan. Mohon hubungi admin."; return(RedirectToAction("Create", "City")); }
public ActionResult AddCity(string kode, string nama, string lstprovince) { string userID = HttpContext.Session.GetString(SessionKeyID); AddCityInputModel data = new AddCityInputModel(); data.UserID = Guid.Parse(userID); data.Kode = kode; data.CityName = nama; data.KodeProvinsi = lstprovince.Split("|")[0].ToString(); JsonConvert.SerializeObject(data); using (var client = new HttpClient()) { client.BaseAddress = new Uri(BaseAPI + "Base/"); //HTTP POST var postTask = client.PostAsJsonAsync<AddCityInputModel>("AddCity", data); postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { return RedirectToAction("Index", "City"); } else { TempData["CustomError"] = "Fail to add data. Please contact administrator."; } } TempData["CustomError"] = "Server Error. Please contact administrator."; return RedirectToAction("Create", "City"); }
public ActionResult <AddCityResponseModel> AddCity([FromBody] AddCityInputModel data) { AddCityResponseModel res = new AddCityResponseModel(); try { CityBL bl = new CityBL(db); var res2 = bl.Save(data); res.data = res2; res.Message = res.Message; res.Response = true; return(res); } catch (Exception ex) { res.Message = ex.Message; res.Response = false; return(res); } }
public AddCityOutputModel Save(AddCityInputModel data) { CityRepository repo = new CityRepository(db); City temp = new City(); var tempKodeProvinsi = Convert.ToInt32(data.KodeProvinsi); temp.CityName = data.CityName; temp.Kode = Convert.ToInt32(data.Kode); temp.KodeProvinsi = Convert.ToInt32(data.KodeProvinsi); temp.ProvinceID = db.Province.Where(x => x.Kode == tempKodeProvinsi).FirstOrDefault().ID; temp.CreateByUserID = data.UserID; temp.CreateDate = DateTime.Now; var res = repo.Insert(temp); AddCityOutputModel output = new AddCityOutputModel(); output.CityID = res.ID; output.CityName = data.CityName; return(output); }