public IActionResult Create(Naručitelj naručitelj) { if (ModelState.IsValid) { try { context.Add(naručitelj); context.SaveChanges(); TempData[Constants.Message] = $"Naručitelj broj: {naručitelj.OibNaručitelja} uspješno dodan!"; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.CompleteExceptionMessage()); return(View(naručitelj)); } } else { return(View(naručitelj)); } }
public async Task <IActionResult> Create(JedanNaručiteljViewModel model) { Naručitelj narucitelj = new Naručitelj { OibNaručitelja = model.oibNaručitelj, NazivNaručitelja = model.nazivNaručitelj, AdresaNaručitelja = model.adresaNaručitelj, PoštanskiBrojNaručitelja = model.poštanskiBrojNaručitelj }; ctx.Add(narucitelj); await ctx.SaveChangesAsync(); var addedItem = await Get(narucitelj.OibNaručitelja); return(CreatedAtAction(nameof(Get), new { oib = narucitelj.OibNaručitelja }, addedItem.Value)); }
public async Task <IActionResult> Update(string id, int page = 1, int sort = 1, bool ascending = true) { try { Naručitelj naručitelj = await context.Naručiteljs.FindAsync(id); if (naručitelj == null) { return(NotFound($"Ne postoji naručitelj čiji je id {id}")); } bool ok = await TryUpdateModelAsync <Naručitelj>(naručitelj, "", d => d.NazivNaručitelja, d => d.AdresaNaručitelja, d => d.PoštanskiBrojNaručitelja); if (ok) { ViewBag.Page = page; ViewBag.Sort = sort; ViewBag.Ascending = ascending; try { await context.SaveChangesAsync(); TempData[Constants.Message] = $"Naručitelj {naručitelj.NazivNaručitelja} uspješno ažuriran."; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index), new { page = page, sort = sort, ascending = ascending })); } catch (Exception exc) { ModelState.AddModelError(string.Empty, exc.CompleteExceptionMessage()); return(View(naručitelj)); } } else { ModelState.AddModelError(string.Empty, "Podatke nije moguće povezati"); return(View(naručitelj)); } } catch (Exception exc) { TempData[Constants.Message] = exc.CompleteExceptionMessage(); TempData[Constants.ErrorOccurred] = true; return(RedirectToAction(nameof(Edit), id)); } }