public IActionResult EditFood(EditFoodViewModel model) { //Update food var food = _context.Matratts.SingleOrDefault(x => x.MatrattId == model.Food.MatrattId); food.MatrattNamn = model.Food.MatrattNamn; food.MatrattTyp = model.Food.MatrattTyp; food.Beskrivning = model.Food.Beskrivning; food.Pris = model.Food.Pris; //Delete old ingredients _context.MatrattProdukts.Where(x => x.MatrattId == model.Food.MatrattId).DeleteFromQuery(); //Insert new ingredients foreach (var ingredient in model.IngredientsChecked) { var foodIngredient = new MatrattProdukt() { MatrattId = food.MatrattId, ProduktId = ingredient }; _context.MatrattProdukts.Add(foodIngredient); } _context.SaveChanges(); //TODO En vy "ConfirmEdit" return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Edit(EditFoodViewModel model) { if (ModelState.IsValid) { if (await _foodServices.EditFood(model)) { return(RedirectToAction(actionName: "Profile", controllerName: "Food", new { foodId = model.FoodId })); } } return(View(model)); }
public virtual ActionResult Edit(string id) { if (string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException("id", "Id cannot be null"); var food = _foodService.GetFood(id); var categories = _foodService.GetCategories(); var model = new EditFoodViewModel { Food = food, Categories = categories.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList() }; return View(model); }
public async Task <bool> EditFood(EditFoodViewModel foodEdit) { try { Food food = await GetFood(foodEdit.FoodId); _context.Foods.Attach(food); if (foodEdit.ImagesChange.Count > 0) { foreach (FoodImage img in food.FoodImages.ToList()) { string filePath = Path.Combine(path1: _webEnv.WebRootPath, path2: "images", path3: "foods", img.FileName); File.Delete(filePath); } food.FoodImages.Clear(); foreach (IFormFile img in foodEdit.ImagesChange) { string fileName = $"{Guid.NewGuid().ToString()}_{img.FileName}"; string filePath = Path.Combine(path1: _webEnv.WebRootPath, path2: "images", path3: "foods", fileName); using (FileStream fs = new FileStream(filePath, FileMode.Create)) { await img.CopyToAsync(fs); } FoodImage newImage = new FoodImage() { FileName = fileName, ImagesId = Guid.NewGuid().ToString(), FoodId = foodEdit.FoodId }; food.FoodImages.Add(newImage); } } food.IsActive = foodEdit.IsActive; food.Name = foodEdit.FoodName; food.Price = foodEdit.Price; await _context.SaveChangesAsync(); return(true); } catch (Exception ex) { return(false); } }
public async Task <IActionResult> Edit(string foodId) { Food food = await _foodServices.GetFood(foodId); if (food == null) { return(NotFound()); } EditFoodViewModel foodEdit = new EditFoodViewModel() { FoodName = food.Name, Images = food.FoodImages.ToList(), IsActive = food.IsActive, Price = food.Price, FoodId = food.FoodId }; return(View(foodEdit)); }
public IActionResult EditFood(int id) { var food = _context.Matratts .Include(m => m.MatrattTypNavigation) .Include(m => m.MatrattProdukts) .ThenInclude(mp => mp.Produkt) .SingleOrDefault(x => x.MatrattId == id); List <CheckedIngredientsViewModel> checkedIngredients = new List <CheckedIngredientsViewModel>(); var ingredientList = _context.Produkts.ToList(); foreach (var ingredient in ingredientList) { var checkedIngredient = new CheckedIngredientsViewModel(); checkedIngredient.Ingredient = ingredient; var result = food.MatrattProdukts.SingleOrDefault(x => x.ProduktId == ingredient.ProduktId); if (result != null) { checkedIngredient.IsChecked = true; } else { checkedIngredient.IsChecked = false; } checkedIngredients.Add(checkedIngredient); } var model = new EditFoodViewModel() { Food = food, AllFoodTypes = _context.MatrattTyps.ToList(), CheckedIngredients = checkedIngredients }; return(View(model)); }
public virtual ActionResult Edit(EditFoodViewModel model) { if (string.IsNullOrWhiteSpace(model.Food.Name)) { ModelState.AddModelError("Food.Name", "Food Name is required"); } if (string.IsNullOrWhiteSpace(model.Food.Image)) { ModelState.AddModelError("Food.Image", "Image is required"); } if (model.Food.GroupId <= 0) { ModelState.AddModelError("Food.GroupId", "Select a category"); } if (model.Food.GmWt_3 < 0) { ModelState.AddModelError("Food.GmWt_3", "Required field"); } if (string.IsNullOrWhiteSpace(model.Food.GmWt_Desc3)) { ModelState.AddModelError("Food.GmWt_Desc3", "Required field"); } if(ModelState.IsValid) { _foodService.Update(model.Food); return RedirectToAction(MVC.Admin.Food.Index()); } var categories = _foodService.GetCategories(); model.Categories = categories.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList(); return View(model); }