public async Task <IActionResult> Edit(int id, [Bind("Id,Name,NumberCAS,HazardClass,MaximumPermissibleConcentrationOneTimeMaximum,MaximumPermissibleConcentrationDailyAverage,ApproximateSafeExposureLevel")] AirContaminant airContaminant) { if (id != airContaminant.Id) { return(NotFound()); } if (ModelState.IsValid) { HttpResponseMessage response = await _HttpApiClient.PutAsJsonAsync( $"api/AirContaminants/{airContaminant.Id}", airContaminant); string OutputViewText = await response.Content.ReadAsStringAsync(); OutputViewText = OutputViewText.Replace("<br>", Environment.NewLine); try { response.EnsureSuccessStatusCode(); } catch { dynamic errors = JsonConvert.DeserializeObject <dynamic>(OutputViewText); foreach (Newtonsoft.Json.Linq.JProperty property in errors.Children()) { ModelState.AddModelError(property.Name, property.Value[0].ToString()); } return(View(airContaminant)); } airContaminant = await response.Content.ReadAsAsync <AirContaminant>(); return(RedirectToAction(nameof(Index))); } return(View(airContaminant)); }
public async Task <IActionResult> Edit(int?id) { AirContaminant airContaminant = null; HttpResponseMessage response = await _HttpApiClient.GetAsync($"api/AirContaminants/{id.ToString()}"); if (response.IsSuccessStatusCode) { airContaminant = await response.Content.ReadAsAsync <AirContaminant>(); } return(View(airContaminant)); }
public async Task <IActionResult> PostAirContaminant([FromBody] AirContaminant airContaminant) { var airContaminants = _context.AirContaminant.AsNoTracking().ToList(); if (_context.AirContaminant.AsNoTracking().FirstOrDefault(a => a.Name == airContaminant.Name) != null) { ModelState.AddModelError("Name", "An object with this value already exists!"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.AirContaminant.Add(airContaminant); await _context.SaveChangesAsync(); return(CreatedAtAction("GetAirContaminant", new { id = airContaminant.Id }, airContaminant)); }
public async Task <IActionResult> PutAirContaminant([FromRoute] int id, [FromBody] AirContaminant airContaminant) { var airContaminants = _context.AirContaminant.AsNoTracking().ToList(); if (_context.AirContaminant.AsNoTracking().FirstOrDefault(a => a.Name == airContaminant.Name && a.Id != airContaminant.Id) != null) { ModelState.AddModelError("Name", "An object with this value already exists!"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != airContaminant.Id) { return(BadRequest()); } _context.Entry(airContaminant).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AirContaminantExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }