//[ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(string back, int id) { back = Request.Form["back"]; CalendarFoodItem foodItem = _calendarDatabase.FoodItems.Find(id); _calendarDatabase.FoodItems.Remove(foodItem); _calendarDatabase.SaveChanges(); return(Redirect("/CalendarFood/Add/" + back)); }
public IActionResult Delete(string back, int id) { CalendarFoodItem foodItem = _calendarDatabase.FoodItems.Find(id); if (foodItem == null) { return(NotFound()); } TempData["back"] = back; return(View(foodItem)); }
public IActionResult Create(int year, string month, int day, int foodId, int mealId) { // TODO PARSE TRY var date = new DateTime(year, int.Parse(month), day); ViewData["Date"] = date; Food food = _calendarDatabase.Foods.Find(foodId); Meal meal = _calendarDatabase.Meals.Find(mealId); if (food == null || meal == null) { TempData["Error"] = "Wrong input"; return(Redirect( string.Format("/calendarFood/add/{0}/{1}/{2}/{3}", year, month, day, mealId))); } // check if already exist and then update it CalendarFoodItem foodItem = _calendarDatabase.FoodItems.Where(cfi => cfi.CalendarDate.Date == date.Date && cfi.Meal.MealID == mealId && cfi.Food.FoodID == food.FoodID).SingleOrDefault(); string GramUnitName = "gram"; WeightType gram = _calendarDatabase.WeightTypes.Where(wt => wt.UnitName == GramUnitName).FirstOrDefault(); if (foodItem == null) { foodItem = new CalendarFoodItem() { Food = food, Meal = meal, Weight = decimal.ToInt32(food.Weight), CalendarDate = date }; _calendarDatabase.FoodItems.Add(foodItem); } else { foodItem.Weight += decimal.ToInt32(food.Weight); } _calendarDatabase.SaveChanges(); TempData["Success"] = "Created."; return(Redirect( string.Format("/calendarFood/add/{0}/{1}/{2}/{3}", year, month, day, mealId))); }
public List <SelectListItem> CreateSelectedListItems(CalendarFoodItem fi) { var foodWeightTypes = new List <SelectListItem>(); // Gram Weighttype bool isGramSelected = fi.SelectedFoodWeightType == null; var gram = GetDefaultGramSelectListItem(isGramSelected); foodWeightTypes.Add(gram); foreach (var fwt in fi.Food.FoodWeightTypes) { var item = new SelectListItem { Text = fwt.WeightType.UnitName, Value = fwt.WeightTypeId.ToString() }; if (!isGramSelected && fi.SelectedFoodWeightType.FoodWeightTypeID == fwt.FoodWeightTypeID) { item.Selected = true; } foodWeightTypes.Add(item); } return(foodWeightTypes); }