public IActionResult Dodaj() { var model = new JeloDodajVM(); GenerisiCmbStavke(model); return(View(model)); }
public IActionResult Uredi(int jeloID) { JeloDodajVM model = new JeloDodajVM { Jelo = _db.Jelo.Find(jeloID) }; return(View(nameof(Dodaj), model)); }
public IActionResult Dodaj() { JeloDodajVM model = new JeloDodajVM { Jelo = new Jelo() }; return(View(model)); }
private void GenerisiCmbStavke(JeloDodajVM model) { model.sastojciLista = db.Sastojci.Select(x => new SelectListItem { Text = x.Naziv, Value = x.Id.ToString() }).ToList(); model.vrstaJelaLista = db.VrstaJela.Select(x => new SelectListItem { Text = x.Naziv, Value = x.Id.ToString() }).ToList(); }
public IActionResult DodajSave(JeloDodajVM model) { if (ModelState.IsValid) { Jelo jelo = db.Jelo.Find(model.JeloId); if (jelo == null) { jelo = new Jelo(); db.Add(jelo); } string uniqueFileName = null; if (model.Slika != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Slika.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); model.Slika.CopyTo(new FileStream(filePath, FileMode.Create)); } jelo.Cijena = model.Cijena; jelo.Naziv = model.Naziv; jelo.Opis = model.Opis; jelo.VrstaJelaID = model.VrstaJelaId; jelo.Slika = uniqueFileName; db.SaveChanges(); Jelo x = db.Jelo.Where(y => y.Id == jelo.Id).SingleOrDefault(); foreach (int s in model.sastojciOdabrani) { ImaSastojke iS = new ImaSastojke { JeloId = x.Id, SastojciId = s }; db.Add(iS); } db.SaveChanges(); return(RedirectToAction("List")); } else { GenerisiCmbStavke(model); return(View("Dodaj", model)); } }
public IActionResult Snimi(JeloDodajVM model) { if (!ModelState.IsValid) { return(View("Dodaj", model)); } Jelo jelo = null; if (model.Jelo.Id != 0) { jelo = model.Jelo; _db.Jelo.Update(jelo); _db.SaveChanges(); } else { jelo = new Jelo(); jelo = model.Jelo; _db.Jelo.Add(jelo); _db.SaveChanges(); } return(RedirectToAction("Index")); }