public async Task <IActionResult> Update(int id, int page = 1, int sort = 1, bool ascending = true) { try { VrsteGoriva vrsteGoriva = await _ctx.VrsteGoriva.FindAsync(id); if (vrsteGoriva == null) { return(NotFound($"Ne postoji vrsteGoriva s oznakom {id}")); } ViewBag.Page = page; ViewBag.Sort = sort; ViewBag.Ascending = ascending; bool ok = await TryUpdateModelAsync <VrsteGoriva>(vrsteGoriva, "", b => b.IdVrsteGoriva, b => b.Naziv); if (ok) { try { TempData[Constants.Message] = $"vrsteGoriva {vrsteGoriva.Naziv} uspjesno azurirana.*"; TempData[Constants.ErrorOccurred] = false; await _ctx.SaveChangesAsync(); return(RedirectToAction(nameof(Index), new { page, sort, ascending })); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); return(View(vrsteGoriva)); } } else { ModelState.AddModelError(string.Empty, "Podatke o vrsteGoriva nije moguce povezati."); return(View(vrsteGoriva)); } } catch (Exception e) { TempData[Constants.Message] = "Pogreska prilikom azuriranja vrsteGoriva." + e.Message; TempData[Constants.ErrorOccurred] = true; return(RedirectToAction(nameof(Edit), new { id, page, sort, ascending })); } }
public IActionResult Create(VrsteGoriva vrsteGoriva) { if (ModelState.IsValid) { try { _ctx.Add(vrsteGoriva); _ctx.SaveChanges(); TempData[Constants.Message] = $"Drzava {vrsteGoriva.Naziv} uspjesno dodana.*"; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); return(View(vrsteGoriva)); } } else { return(View(vrsteGoriva)); } }