コード例 #1
0
        public async Task <IActionResult> Create(AddDefaultMealViewModel addDefaultMealViewModel)
        {
            if (ModelState.IsValid)
            {
                WeatherType   newWeatherType      = _context.WeatherTypes.Single(w => w.ID == addDefaultMealViewModel.WeatherTypeID);
                CookingMethod newCookingMethod    = _context.CookingMethods.Single(m => m.ID == addDefaultMealViewModel.CookingMethodID);
                CookingMethod newAltCookingMethod = _context.CookingMethods.Single(a => a.ID == addDefaultMealViewModel.AltCookingMethodID);
                CookingTime   newCookingTime      = _context.CookingTimes.Single(t => t.ID == addDefaultMealViewModel.CookingTimeID);
                PrepTime      newPrepTime         = _context.PrepTimes.Single(p => p.ID == addDefaultMealViewModel.PrepTimeID);

                //Add the new default meal to the default meal table
                DefaultMeal newDefaultMeal = new DefaultMeal
                {
                    Name             = addDefaultMealViewModel.Name,
                    Description      = addDefaultMealViewModel.Description,
                    WeatherType      = newWeatherType,
                    CookingMethod    = newCookingMethod,
                    AltCookingMethod = newAltCookingMethod,
                    CookingTime      = newCookingTime,
                    PrepTime         = newPrepTime
                };

                _context.DefaultMeals.Add(newDefaultMeal);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(addDefaultMealViewModel));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(EditDefaultMealViewModel editDefaultMealViewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    WeatherType   newWeatherType      = _context.WeatherTypes.Single(w => w.ID == editDefaultMealViewModel.WeatherTypeID);
                    CookingMethod newCookingMethod    = _context.CookingMethods.Single(m => m.ID == editDefaultMealViewModel.CookingMethodID);
                    CookingMethod newAltCookingMethod = _context.CookingMethods.Single(a => a.ID == editDefaultMealViewModel.AltCookingMethodID);
                    CookingTime   newCookingTime      = _context.CookingTimes.Single(t => t.ID == editDefaultMealViewModel.CookingTimeID);
                    PrepTime      newPrepTime         = _context.PrepTimes.Single(p => p.ID == editDefaultMealViewModel.PrepTimeID);

                    //Update the default meal in the default meal table
                    DefaultMeal editDefaultMeal = new DefaultMeal
                    {
                        ID               = editDefaultMealViewModel.DefaultMealID,
                        Name             = editDefaultMealViewModel.Name,
                        Description      = editDefaultMealViewModel.Description,
                        WeatherType      = newWeatherType,
                        CookingMethod    = newCookingMethod,
                        AltCookingMethod = newAltCookingMethod,
                        CookingTime      = newCookingTime,
                        PrepTime         = newPrepTime
                    };

                    _context.Update(editDefaultMeal);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DefaultMealExists(editDefaultMealViewModel.DefaultMealID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(editDefaultMealViewModel));
        }