public ActionResult EditProfile(ProfileViewModel profileViewModel) { if (ModelState.IsValid) { ValidateProfile(profileViewModel); TempData["Message"] = "Your meal entry was successfully updated!"; using (var mealProfilerContext = new MealProfilerContext()) { var profile = mealProfilerContext.Profiles.SingleOrDefault(p => p.ProfileId == profileViewModel.ProfileId); if (profile != null) { profile.MealGoal = profileViewModel.MealGoal; profile.PrepTime = profileViewModel.PrepTime; profile.MealCost = profileViewModel.MealCost; profile.MealAuthor = profileViewModel.MealAuthor; profile.SpinachQuantity = profileViewModel.SpinachQuantity; profile.TomatoQuantity = profileViewModel.TomatoQuantity; profile.SpinachCheckbox = profileViewModel.SpinachCheckbox; profile.TomatoCheckbox = profileViewModel.TomatoCheckbox; profile.Notes = profileViewModel.Notes; mealProfilerContext.SaveChanges(); return(RedirectToAction("Index")); } return(new HttpNotFoundResult()); } } SetupMealsSelectListItems(); return(View(profileViewModel)); }
public ActionResult AddProfile(ProfileViewModel profileViewModel) { if (ModelState.IsValid) { ValidateProfile(profileViewModel); TempData["Message"] = "Your meal entry was successfully added!"; using (var mealProfilerContext = new MealProfilerContext()) { var profile = new Profile { MealGoal = profileViewModel.MealGoal, PrepTime = profileViewModel.PrepTime, MealCost = profileViewModel.MealCost, MealAuthor = profileViewModel.MealAuthor, SpinachQuantity = profileViewModel.SpinachQuantity, TomatoQuantity = profileViewModel.TomatoQuantity, SpinachCheckbox = profileViewModel.SpinachCheckbox, TomatoCheckbox = profileViewModel.TomatoCheckbox, Notes = profileViewModel.Notes }; mealProfilerContext.Profiles.Add(profile); mealProfilerContext.SaveChanges(); } return(RedirectToAction("Index")); } // this will add the list of our meals enum to the dropdownfor: so in the future, don't manually add them to a dropdown form element. use the DropDownFor method as seen here. along with the enum. SetupMealsSelectListItems(); return(View("AddEditProfile", profileViewModel)); }
public ActionResult DeleteProfile(ProfileViewModel profileViewModel) { using (var mealProfilerContext = new MealProfilerContext()) { var profile = mealProfilerContext.Profiles.SingleOrDefault(p => p.ProfileId == profileViewModel.ProfileId); if (profile != null) { mealProfilerContext.Profiles.Remove(profile); mealProfilerContext.SaveChanges(); return(RedirectToAction("Index")); } } return(new HttpNotFoundResult()); }