// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(MealType).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MealTypeExists(MealType.ID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. /*public async Task<IActionResult> OnPostAsync() * { * if (!ModelState.IsValid) * { * return Page(); * } * * var entry = _context.Add(new Recipe()); * entry.CurrentValues.SetValues(RecipeVM); * await _context.SaveChangesAsync(); * return RedirectToPage("./Index"); * }*/ public async Task <IActionResult> OnPostAsync(string[] selectedMealTypes) { var newRecipe = new Recipe(); if (selectedMealTypes != null) { newRecipe.RecipeAssignments = new List <RecipeAssignment>(); foreach (var mealType in selectedMealTypes) { var mealTypeToAdd = new RecipeAssignment { MealTypeID = int.Parse(mealType) }; newRecipe.RecipeAssignments.Add(mealTypeToAdd); } } if (await TryUpdateModelAsync <Recipe>( newRecipe, "Recipe", r => r.Name, r => r.NumberOfServings, r => r.LastTimeServed)) { _context.Recipes.Add(newRecipe); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateAssignedMealTypeData(_context, newRecipe); return(Page()); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.QuantityInRecipes.Add(QuantityInRecipe); await _context.SaveChangesAsync(); return(RedirectToPage("../Recipes/Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } QuantityInRecipe = await _context.QuantityInRecipes.FindAsync(id); if (QuantityInRecipe != null) { _context.QuantityInRecipes.Remove(QuantityInRecipe); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { /*if (!ModelState.IsValid) * { * return Page(); * } * * _context.Attach(Ingredient).State = EntityState.Modified; * * try * { * await _context.SaveChangesAsync(); * } * catch (DbUpdateConcurrencyException) * { * if (!IngredientExists(Ingredient.ID)) * { * return NotFound(); * } * else * { * throw; * } * } * * return RedirectToPage("./Index");*/ var ingredientToUpdate = await _context.Ingredients.FindAsync(id); if (ingredientToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Ingredient>( ingredientToUpdate, "ingredient", i => i.Name, i => i.TypeOfFood)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } /* * Ingredient = await _context.Ingredients.FindAsync(id); * * if (Ingredient != null) * { * _context.Ingredients.Remove(Ingredient); * await _context.SaveChangesAsync(); * } * * return RedirectToPage("./Index");*/ var ingredient = await _context.Ingredients.FindAsync(id); if (ingredient == null) { return(NotFound()); } try { _context.Ingredients.Remove(ingredient); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } /* * Recipe = await _context.Recipes.FindAsync(id); * * if (Recipe != null) * { * _context.Recipes.Remove(Recipe); * await _context.SaveChangesAsync(); * } * * return RedirectToPage("./Index");*/ var recipe = await _context.Recipes.FindAsync(id); if (recipe == null) { return(NotFound()); } try { _context.Recipes.Remove(recipe); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch { return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { /*if (!ModelState.IsValid) * { * return Page(); * } * * _context.Ingredients.Add(Ingredient); * await _context.SaveChangesAsync(); * * return RedirectToPage("./Index");*/ /*var emptyIngredient = new Ingredient(); * * if (await TryUpdateModelAsync<Ingredient>( * emptyIngredient, * "ingredient", * i => i.Name, i => i.TypeOfFood)) * { * _context.Ingredients.Add(emptyIngredient); * await _context.SaveChangesAsync(); * return RedirectToPage("./Index"); * } * * return Page();*/ if (!ModelState.IsValid) { return(Page()); } var entry = _context.Add(new Ingredient()); entry.CurrentValues.SetValues(IngredientVM); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. //public async Task<IActionResult> OnPostAsync(int id) public async Task <IActionResult> OnPostAsync(int?id, string[] selectedMealTypes) { /*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");*/ /*bylo tylko to * var recipeToUpdate = await _context.Recipes.FindAsync(id); * * if (recipeToUpdate == null) * { * return NotFound(); * } * * if(await TryUpdateModelAsync<Recipe>( * recipeToUpdate, * "recipe", * r => r.Name, r => r.NumberOfServings, r => r.LastTimeServed)) * { * await _context.SaveChangesAsync(); * return RedirectToPage("./Index"); * } * * return Page();*/ if (id == null) { return(NotFound()); } var recipeToUpdate = await _context.Recipes .Include(r => r.RecipeAssignments) .ThenInclude(r => r.MealType) .FirstOrDefaultAsync(s => s.RecipeID == id); if (recipeToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Recipe>( recipeToUpdate, "Recipe", r => r.Name, r => r.NumberOfServings, r => r.LastTimeServed)) { UpdateRecipeMealTypes(_context, selectedMealTypes, recipeToUpdate); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } UpdateRecipeMealTypes(_context, selectedMealTypes, recipeToUpdate); PopulateAssignedMealTypeData(_context, recipeToUpdate); return(Page()); }