public ActionResult EditDish(int id) { try { var dishInDb = dishService.GetDishByID(id); var ingredients = ingredientService.GetAllIngredients().ToList(); foreach (var ing in ingredients) { if (dishInDb.Ingredients.FirstOrDefault(ingredient => ingredient.ID == ing.ID) != null) { ing.IsSelected = true; } } var dishViewModel = new SaveDishViewModel() { DishDto = dishInDb, DishTypeDtos = dishService.GetAllDishTypes().ToList(), Ingredients = ingredients }; return(View("SaveDishForm", dishViewModel)); } catch (Exception e) { return(new HttpStatusCodeResult(500)); } }
public ActionResult SaveDish(SaveDishViewModel dish) { if (!ModelState.IsValid) { var dishViewModel = new SaveDishViewModel() { DishDto = dish.DishDto, Ingredients = ingredientService.GetAllIngredients().ToList(), DishTypeDtos = dishService.GetAllDishTypes().ToList() }; return(View("SaveDishForm", dishViewModel)); } try { if (dish.DishDto.DishID == 0) { DishDto newDish = new DishDto() { Name = dish.DishDto.Name, Description = dish.DishDto.Description, CreatedOn = DateTime.Now, Price = dish.DishDto.Price, IsActive = true, DishType = dish.DishDto.DishType, Ingredients = dish.Ingredients.Where(ing => ing.IsSelected).ToList() }; dishService.CreateNewDish(newDish); return(RedirectToAction("Index")); } //edit dish DishDto editDishDto = new DishDto() { DishID = dish.DishDto.DishID, Name = dish.DishDto.Name, Description = dish.DishDto.Description, Price = dish.DishDto.Price, DishType = dish.DishDto.DishType, Ingredients = dish.Ingredients.Where(ing => ing.IsSelected).ToList(), }; dishService.UpdateDish(editDishDto); return(RedirectToAction("Index")); } catch (NullReferenceException e) { return(new HttpStatusCodeResult(500)); } }
public ActionResult AddDish() { try { var dishViewModel = new SaveDishViewModel() { DishDto = new DishDto(), Ingredients = ingredientService.GetAllIngredients().ToList(), DishTypeDtos = dishService.GetAllDishTypes().ToList() }; return(View("SaveDishForm", dishViewModel)); } catch (Exception e) { Console.WriteLine(e); throw; } }