コード例 #1
0
        public ActionResult ProfileDetail(int id)
        {
            using (var mealProfilerContext = new MealProfilerContext())
            {
                var profile = mealProfilerContext.Profiles.SingleOrDefault(p => p.ProfileId == id);
                if (profile != null)
                {
                    var profileViewModel = new ProfileViewModel
                    {
                        ProfileId       = profile.ProfileId,
                        MealGoal        = profile.MealGoal,
                        PrepTime        = profile.PrepTime,
                        MealCost        = profile.MealCost,
                        MealAuthor      = profile.MealAuthor,
                        SpinachQuantity = profile.SpinachQuantity,
                        TomatoQuantity  = profile.TomatoQuantity,
                        SpinachCheckbox = profile.SpinachCheckbox,
                        TomatoCheckbox  = profile.TomatoCheckbox,
                        Notes           = profile.Notes
                    };

                    return(View(profileViewModel));
                }
            }

            return(new HttpNotFoundResult());
        }
コード例 #2
0
        public ActionResult Index()
        {
            using (var mealProfilerContext = new MealProfilerContext())
            {
                var profileList = new ProfileListViewModel
                {
                    //Convert each Person to a PersonViewModel
                    Profiles = mealProfilerContext.Profiles.Select(p => new ProfileViewModel
                    {
                        ProfileId       = p.ProfileId,
                        MealGoal        = p.MealGoal,
                        PrepTime        = p.PrepTime,
                        MealCost        = p.MealCost,
                        MealAuthor      = p.MealAuthor,
                        SpinachQuantity = p.SpinachQuantity,
                        TomatoQuantity  = p.TomatoQuantity,
                        SpinachCheckbox = p.SpinachCheckbox,
                        TomatoCheckbox  = p.TomatoCheckbox,
                        Notes           = p.Notes
                    }).ToList()
                };
                profileList.TotalProfiles = profileList.Profiles.Count;

                return(View(profileList));
            }
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        public ActionResult ProfileEdit(int id)
        {
            using (var mealProfilerContext = new MealProfilerContext())
            {
                var profile = mealProfilerContext.Profiles.SingleOrDefault(p => p.ProfileId == id);
                if (profile != null)
                {
                    var profileViewModel = new ProfileViewModel
                    {
                        ProfileId       = profile.ProfileId,
                        MealGoal        = profile.MealGoal,
                        PrepTime        = profile.PrepTime,
                        MealCost        = profile.MealCost,
                        MealAuthor      = profile.MealAuthor,
                        SpinachQuantity = profile.SpinachQuantity,
                        TomatoQuantity  = profile.TomatoQuantity,
                        SpinachCheckbox = profile.SpinachCheckbox,
                        TomatoCheckbox  = profile.TomatoCheckbox,
                        Notes           = profile.Notes
                    };

                    // 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));
                }
            }

            return(new HttpNotFoundResult());
        }
コード例 #5
0
        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));
        }
コード例 #6
0
        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());
        }