コード例 #1
0
        public ActionResult OnPostCreate(Recipe Recipe, IList <Ingredient> Ingredients)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            //var ingredients = JsonConvert.DeserializeObject<IList<Ingredient>>(ingredients);


            _context.Recipe.Add(Recipe);
            _context.SaveChangesAsync();
            int id = Recipe.RecipeID;

            int i = 0;

            while (i < Ingredients.Count)
            {
                Ingredients[i].RecipeID = id;
                _context.Ingredient.Add(Ingredients[i]);
                i = i + 1;
            }


            Recipe.CreatedDate = DateTime.UtcNow;
            _context.SaveChangesAsync();

            return(new EmptyResult());
        }
コード例 #2
0
        public ActionResult OnPostEdit(Recipe Recipe, IList <Ingredient> Ingredients)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            int id = Recipe.RecipeID;

            if (Recipe.CreatedDate == DateTime.MinValue)
            {
                Recipe.CreatedDate = DateTime.UtcNow;
            }

            _context.Attach(Recipe).State = EntityState.Modified;


            int i = 0;

            while (i < (Ingredients.Count))
            {
                Ingredients[i].RecipeID = id;
                if (IngredientExists(Ingredients[i].IngredientID))
                {
                    _context.Attach(Ingredients[i]).State = EntityState.Modified;
                    _context.SaveChangesAsync();
                    i = i + 1;
                }
                else
                {
                    _context.Ingredient.Add(Ingredients[i]);
                    _context.SaveChangesAsync();
                    i = i + 1;
                }
            }

            try {
                _context.SaveChangesAsync();
            }



            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeExists(Recipe.RecipeID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(new EmptyResult());
            //return RedirectToPage("./Index");
        }
コード例 #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Recipe).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeExists(Recipe.RecipeID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
コード例 #4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Recipe.Add(Recipe);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
コード例 #5
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Recipe = await _context.Recipe.FindAsync(id);

            if (Recipe != null)
            {
                _context.Recipe.Remove(Recipe);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }