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 IActionResult CreateDish(CreateDishViewModel data) { if (ModelState.IsValid) { data.DishIngredients = _dishService.FetchIngredientsFromSession(); _dishService.CreateNewDish(data.Dish, data.DishIngredients); _sessionService.Clear(); return(RedirectToAction("ViewDishes", "Admin")); } var model = new CreateDishViewModel { DishIngredients = _dishService.FetchIngredientsFromSession(), Ingredients = _dishService.FetchDishIngredients(), Categories = _dishService.FetchDishCategories() }; return(View(model)); }