public ActionResult CadastrarProduto(Produto p, int ddlCategoria, string[] selectedIngredientes) { if (!IsAdmin()) { return(RedirectToAction("Login")); } // ----- verificando admin if (ModelState.IsValid) { _db.Produtos.Add(p); p.CategoriaId = ddlCategoria; p.ValorAntigo = "0"; _db.SaveChanges(); foreach (var id in selectedIngredientes) { var prodHasIng = new ProdutoHasIngrediente(); prodHasIng.IngredienteId = Convert.ToInt32(id); prodHasIng.ProdutoId = p.IdProduto; prodHasIng.Quantidade = 0; _db.ProdutoHasIngredientes.Add(prodHasIng); _db.SaveChanges(); } } else { return(View()); } ViewBag.Ingredientes = _db.Ingredientes.ToList(); ViewBag.Categoria = new SelectList(_db.Categorias.ToList(), "IdCategoria", "Nome"); ModelState.Clear(); return(View()); }
public ActionResult AdicionarItemPRoduto(int IdProduct, ProdutoEditar pe) { ProdutoHasIngrediente p = new ProdutoHasIngrediente(); p.IngredienteId = pe.ProdutoHasIngredientes.Ingrediente.IdIngrediente; p.ProdutoId = IdProduct; p.Quantidade = pe.ProdutoHasIngredientes.Quantidade; _db.ProdutoHasIngredientes.Add(p); _db.SaveChanges(); return(RedirectToAction("EditarProduto")); }
public ActionResult EditarProduto(ProdutoHasIngrediente phin) { //if (!IsAdmin()) // return RedirectToAction("Login"); var prods = (from p in _db.Produtos join phi in _db.ProdutoHasIngredientes on p.IdProduto equals phi.ProdutoId join i in _db.Ingredientes on phi.IngredienteId equals i.IdIngrediente select new { proId = p.IdProduto, proNome = p.Nome, proIngrediente = i.Nome, proHasQuantidade = phi.Quantidade, proHasIngProId = phi.ProdutoId, proHasIngId = phi.IdProdutoHasCategoria, idIngredienteHas = phi.IngredienteId, idIngrediente = i.IdIngrediente }).ToList(); var listaProEdit = new List <ProdutoEditar>(); foreach (var item in prods) { ProdutoEditar pe = new ProdutoEditar(); pe.Produtos = new Produto(); pe.Ingredientes = new Ingrediente(); pe.ProdutoHasIngredientes = new ProdutoHasIngrediente(); pe.Produtos.IdProduto = item.proId; pe.Produtos.Nome = item.proNome; pe.Ingredientes.Nome = item.proIngrediente; pe.ProdutoHasIngredientes.Quantidade = item.proHasQuantidade; pe.ProdutoHasIngredientes.ProdutoId = item.proHasIngProId; pe.ProdutoHasIngredientes.IdProdutoHasCategoria = item.proHasIngId; pe.ProdutoHasIngredientes.IngredienteId = item.idIngrediente; pe.Ingredientes.IdIngrediente = item.idIngrediente; listaProEdit.Add(pe); } ViewData["Ingredientes"] = _db.Ingredientes.ToList(); //ViewBag.Ingrediente = new SelectList(_db.Ingredientes.ToList(), "IdIngrediente", "Nome"); if (listaProEdit != null) { ViewData["produ"] = listaProEdit.ToList(); } return(View()); }
//[ValidateAntiForgeryToken] public ActionResult CadastrarProduto(Produto Produto, List <ProdutoRecebeIngredientes> Ingredientes) { // if (!IsAdmin()) // return RedirectToAction("Login"); // ----- verificando admin if (ModelState.IsValid) { _db.Produtos.Add(Produto); //Task<string> task = ImagemUploadService.SalvarImagemProduto(Foto); // Prod.Foto = task.Result; _db.SaveChanges(); foreach (var ings in Ingredientes) { var prodHasIng = new ProdutoHasIngrediente(); prodHasIng.IngredienteId = Convert.ToInt32(ings.idIng); prodHasIng.ProdutoId = Produto.IdProduto; prodHasIng.Quantidade = ings.quantidadeIng; _db.ProdutoHasIngredientes.Add(prodHasIng); _db.SaveChanges(); } } else { ViewBag.Ingredientes = _db.Ingredientes.ToList(); ViewBag.VerificaCategoria = _db.Categorias.ToList(); ViewBag.Categoria = new SelectList(_db.Categorias.ToList(), "IdCategoria", "Nome"); return(Json(Produto)); } ViewBag.Ingredientes = _db.Ingredientes.ToList(); ViewBag.VerificaCategoria = _db.Categorias.ToList(); ViewBag.Categoria = new SelectList(_db.Categorias.ToList(), "IdCategoria", "Nome"); //ModelState.Clear (); return(Json(Produto)); }