public IActionResult Create(PartnerViewModel model) { if (ModelState.IsValid) { Partner p = new Partner(); p.TipPartnera = model.TipPartnera; CopyValues(p, model); try { ctx.Add(p); ctx.SaveChanges(); TempData[Constants.Message] = $"Partner uspješno dodan. Id={p.IdPartnera}"; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception exc) { ModelState.AddModelError(string.Empty, exc.CompleteExceptionMessage()); return(View(model)); } } else { return(View(model)); } }
public IActionResult Delete(int id) { var oprema = ctx.Oprema.Find(id); if (oprema != null) { try { string naziv = oprema.Naziv; ctx.Remove(oprema); ctx.SaveChanges(); var result = new { message = $"Oprema {naziv} sa šifrom {id} uspješno obrisan.", successful = true }; return(Json(result)); } catch (Exception exc) { var result = new { message = "Pogreška prilikom brisanja opreme: " + exc.CompleteExceptionMessage(), successful = false }; return(Json(result)); } } else { return(NotFound($"Oprema sa šifrom {id} ne postoji")); } }
public IActionResult Create(Poslovi poslovi) { logger.LogTrace(JsonSerializer.Serialize(poslovi), new JsonSerializerOptions { IgnoreNullValues = true }); if (ModelState.IsValid) //ako je model valjan može raditi jer smo u leyout dodali jqueryvalidate i validation unbotrusive { try { ctx.Add(poslovi); //kontekst ctx.SaveChanges(); //dodajemo u bazu logger.LogInformation(new EventId(1000), $"Posao {poslovi.Naziv} dodana."); TempData[Constants.Message] = $"Posao {poslovi.Naziv} uspješno dodana."; //pohranjuje podatke u session i brišu se nakon korištenja/stvorili Constants.cs za ove TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception exc) { logger.LogError("Pogreška prilikom dodavanje novog posla: {0}", exc.CompleteExceptionMessage()); ModelState.AddModelError(string.Empty, exc.CompleteExceptionMessage()); PrepareDropDownLists(); return(View(poslovi)); } } else { PrepareDropDownLists(); return(View(poslovi)); } }
public IActionResult Edit(Zaposlenici zaposlenici, int page = 1, int sort = 1, bool ascending = true) { if (zaposlenici == null) { return(NotFound("Nema poslanih podataka")); } bool checkId = ctx.Zaposlenici.Any(m => m.IdZaposlenici == zaposlenici.IdZaposlenici); if (!checkId) { return(NotFound($"Neispravan id zaposlenika: {zaposlenici?.IdZaposlenici}")); } PrepareDropDownLists(); if (ModelState.IsValid) { try { ctx.Update(zaposlenici); ctx.SaveChanges(); TempData[Constants.Message] = "Zaposlenici ažurirano."; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index), new { page, sort, ascending })); } catch (Exception exc) { ModelState.AddModelError(string.Empty, exc.CompleteExceptionMessage()); return(View(zaposlenici)); } } else { return(View(zaposlenici)); } }