public IActionResult Add(AddCategoryViewModel vm) { if (ModelState.IsValid) { CheeseCategory newCategory = new CheeseCategory { Name = vm.Name }; context.Categories.Add(newCategory); context.SaveChanges(); return(Redirect("Index")); } return(View(vm)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (addCategoryViewModel.Name != null) { CheeseCategory newCheeseCategory = new CheeseCategory { Name = addCategoryViewModel.Name }; context.Categories.Add(newCheeseCategory); context.SaveChanges(); return(Redirect("/Category")); } else { return(View("Add")); } }
public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } CheeseCategory theCategory = context .Categories .Include(cat => cat.Cheeses) .SingleOrDefault(cat => cat.ID == id); ViewBag.Title = string.Format("Cheeses in category: {0}", theCategory.Name); return(View("Index", theCategory.Cheeses)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (ModelState.IsValid) { CheeseCategory newCategory = new CheeseCategory { Name = addCategoryViewModel.Name, }; context.Categories.Add(newCategory); context.SaveChanges(); return(Redirect("/Category")); } return(View(addCategoryViewModel)); }
public IActionResult Add(AddCategoryViewModel formInput) { if (!ModelState.IsValid) { return(View("Add")); } //if viewmodel passes validation create a new cheese category and add it to the datatbase context CheeseCategory newCategory = new CheeseCategory { Name = formInput.Name }; context.Categories.Add(newCategory); context.SaveChanges(); return(Redirect("/Category")); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { CheeseCategory newCheeseCategory = context.Categories.Single(c => c.ID == addCheeseViewModel.CategoryID); Cheese newCheese = addCheeseViewModel.CreateCheese(newCheeseCategory); context.Cheeses.Add(newCheese); context.SaveChanges(); return(Redirect("/Cheese")); } return(View(addCheeseViewModel)); }
// .NET CORE default routing. /Controller/Action/id public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } CheeseCategory theCategory = context.Categories.Include(cat => cat.Cheeses).Single(cat => cat.ID == id); // To query for the cheeses from the other side of the relationship: /* * IList<Cheese> theCheeses = context.Cheeses.Include(c => c.Category).Where(c => c.CategoryID==id).ToList(); */ ViewBag.title = "Cheeses in category: " + theCategory.Name; return(View("Index", theCategory.Cheeses)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (ModelState.IsValid) { // Add the new category to my existing categories CheeseCategory newCategory = new CheeseCategory { Name = addCategoryViewModel.Name, }; context.Categories.Add(newCategory); context.SaveChanges(); return(Redirect("/Category/Index")); } return(View(addCategoryViewModel)); }
public IActionResult Index() { if (context.Categories.Count() == 0) { CheeseCategory hard = new CheeseCategory(); CheeseCategory soft = new CheeseCategory(); hard.Name = "Hard"; context.Categories.Add(hard); soft.Name = "Soft"; context.Categories.Add(soft); context.SaveChanges(); } IList <Cheese> cheeses = context.Cheeses.Include(c => c.Category).ToList(); return(View(cheeses)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (ModelState.IsValid) { // Add the new cheese to my existing cheeses CheeseCategory newCategory = new CheeseCategory { Name = addCategoryViewModel.Name, }; context.Categories.Add(newCategory); context.SaveChanges(); return(Redirect("/Category")); } // if !Model.State.IsValid it skips down to here return(View(addCategoryViewModel)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { CheeseCategory cheeseCategory = context.Categories.Single(c => c.ID == addCheeseViewModel.CategoryID); if (ModelState.IsValid) { // Add the new cheese to my existing cheeses Cheese newCheese = new Cheese { Name = addCheeseViewModel.Name, Description = addCheeseViewModel.Description, Category = cheeseCategory }; context.Cheeses.Add(newCheese); context.SaveChanges(); return(Redirect("/Cheese")); } return(View(addCheeseViewModel)); }
public IActionResult Edit(EditCheeseViewModel editCheeseVM) { if (ModelState.IsValid) { Cheese theCheese = context.Cheeses.Single(c => c.ID == editCheeseVM.CheeseID); CheeseCategory category = context.Categories.Single(c => c.ID == editCheeseVM.CategoryID); theCheese.Name = editCheeseVM.Name; theCheese.Description = editCheeseVM.Description; theCheese.CategoryID = editCheeseVM.CategoryID; theCheese.Category = category; context.SaveChanges(); return(Redirect("/")); } return(View(editCheeseVM)); }
//default routing public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } //will retrieve a specific category that has its cheeses list populated //that matches a given ID passed in by the user CheeseCategory theCategory = context.Categories .Include(cat => cat.Cheeses) .Single(cat => cat.ID == id); ViewBag.title = "Cheeses in category: " + theCategory.Name; //passes the list into the View //wouldn't have been populated if not included above //Cheeses is a property we already defined return(View("Index", theCategory.Cheeses)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (!ModelState.IsValid) { return(View(addCategoryViewModel)); } CheeseCategory newCategory = new CheeseCategory { Name = addCategoryViewModel.Name }; // Add newCategory to the DbContext instance and then save it to the DB context.Categories.Add(newCategory); context.SaveChanges(); // Redirect to the Index action of the Category controller return(Redirect("/Category")); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (ModelState.IsValid) { // Add the new category to categories CheeseCategory newCategory = new CheeseCategory { Name = addCategoryViewModel.Name }; context.Categories.Add(newCategory); //must save changes made to database context.SaveChanges(); return(Redirect("/Category")); } return(View(addCategoryViewModel)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (ModelState.IsValid) { // Add the new cheese to my existing cheeses CheeseCategory newCategory = new CheeseCategory { Name = addCategoryViewModel.Name //Description = addCategoryViewModel.Description, //Type = addCategoryViewModel.Type }; context.Categories.Add(newCategory); context.SaveChanges(); return(Redirect("/Category")); } return(View(addCategoryViewModel)); }
public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } CheeseCategory theCategory = context.Categories .Include(category => category.Cheeses) .Single(category => category.ID == id); /* * IList<Cheese> theCheeses = context.Cheese * .Include(cheese => cheese.Category) * .Where(cheese => cheese.CategoryID == id) * .ToList(); */ return(View("Index", theCategory.Cheeses)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (ModelState.IsValid) { // Add the new cheese category to my existing categories CheeseCategory newCategory = new CheeseCategory { Name = addCategoryViewModel.Name, }; context.Categories.Add(newCategory); context.SaveChanges(); //Assignment instructions say to redirect to Index of //Category controller ("/")... it should //be ("/Category"). return(Redirect("/Category")); } return(View(addCategoryViewModel)); }
public IActionResult Edit(EditCheeseViewModel editCheeseViewModel) { Cheese editCheese = context.Cheeses.Single(e => e.ID == editCheeseViewModel.CheeseId); CheeseCategory cheeseCategory = context.Categories.Single(c => c.ID == editCheeseViewModel.CategoryID); if (ModelState.IsValid) { editCheese.Name = editCheeseViewModel.Name; editCheese.Description = editCheeseViewModel.Description; editCheese.Category = cheeseCategory; context.SaveChanges(); return(Redirect("/")); } return(View(editCheeseViewModel)); }
public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } CheeseCategory theCategory = context.Categories .Include(cat => cat.Cheeses) .Single(cat => cat.ID == id); //IList<Cheese> theCheeses = context.Cheeses // .Include(c +> c.Category) // .Where(context => context.CategoryID == id) // .ToList(); ViewBag.title = theCategory.Name + "Cheeses"; return(View("Index", theCategory.Cheeses)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { CheeseCategory newCheeseCategory = _context.Categories.SingleOrDefault(c => c.ID == addCheeseViewModel.CategoryID); Cheese newCheese = new Cheese { Name = addCheeseViewModel.Name, Description = addCheeseViewModel.Description, Category = newCheeseCategory }; // CheeseData.Add(newCheese); _context.Cheeses.Add(newCheese); _context.SaveChanges(); return(Redirect("/Cheeses")); } return(View(addCheeseViewModel)); }
public async Task <IActionResult> Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { CheeseCategory newCheeseCategory = Context.Categories.Single(c => c.ID == addCheeseViewModel.CategoryID); Cheese newCheese = new Cheese() { Name = addCheeseViewModel.Name, Description = addCheeseViewModel.Description, Rating = addCheeseViewModel.Rating, Category = newCheeseCategory, UserID = UserManager.GetUserId(User) }; var isAuthorized = await AuthorizationService.AuthorizeAsync( User, newCheese, CheeseOperations.Create); if (!isAuthorized.Succeeded) { return(Forbid()); } //Cheese newCheese = addCheeseViewModel.CreateCheese(); //newCheese.Category = newCheeseCategory; //context.Cheeses.Add(newCheese); //context.SaveChanges(); Context.Cheeses.Add(newCheese); await Context.SaveChangesAsync(); return(Redirect("/Cheese")); } //AddCheeseViewModel newCheeseViewModel = new AddCheeseViewModel(Context.Categories.ToList()); return(View(addCheeseViewModel)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { CheeseCategory newCheeseCategory = context.Categories.Single(c => c.ID == addCheeseViewModel.CategoryID); Cheese newCheese = new Cheese { Name = addCheeseViewModel.Name, Description = addCheeseViewModel.Description, Rating = addCheeseViewModel.Rating, Category = newCheeseCategory }; context.Cheeses.Add(newCheese); context.SaveChanges(); //built in method to use for saving return(Redirect("/Cheese")); } return(View(addCheeseViewModel)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { //check if model is valid if (ModelState.IsValid) { // if valid, Create a new CheeseCategory object (newCategory) with Name property having the value of Name from the ViewModel CheeseCategory newCategory = new CheeseCategory { Name = addCategoryViewModel.Name, }; //Add newCategory to the database context and save the changes to the db context.Categories.Add(newCategory); context.SaveChanges(); //Redirect to the Index action for CategoryController return(Redirect("/Category")); } //if not valid, return the view and pass the viewmodel into the view return(View(addCategoryViewModel)); }
// /Controller/Action/id public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } CheeseCategory theCategory = context.Categories .Include(cat => cat.Cheeses) .Single(cat => cat.ID == id); ViewBag.title = "Cheese in category: " + theCategory.Name; return(View("Index", theCategory.Cheeses)); }
public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } CheeseCategory theCategory = _context.Categories .Include(cat => cat.Cheeses) .Single(cat => cat.ID == id); //Link syntax query from the other side of the view //IList<Cheeses> theCheeses = _context.Cheeses // .Include(c => c.Category) // .Where(c => c.CategoryID == id) // .ToList(); ViewBag.title = "Cheeses in category " + theCategory.Name; return(View("Index", theCategory.Cheeses)); }
public IActionResult Category(int Id) { if (Id == 0) { return(Redirect("/Category/Index")); } CheeseCategory theCategory = _dbContext.Categories. Include(cat => cat.Cheeses). Single(cat => cat.ID == Id); // To query from the // Other side of the relationship /* * ICollection<Cheese> theChesses = _dbContext.Cheeses. * Include(c=>c.Category). * Where(c.CategoryID == Id).ToList(); */ ViewBag.Title = "Cheeses in Category: " + theCategory.Name; return(View("Index", theCategory.Cheeses)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { CheeseCategory newCheeseCategory = context.Categories.Single(predicate: c => c.ID == addCheeseViewModel.CategoryID); Cheese newCheese = new Cheese { Name = addCheeseViewModel.Name, Description = addCheeseViewModel.Description, Category = newCheeseCategory, Rating = addCheeseViewModel.Rating }; context.Cheeses.Add(newCheese); context.SaveChanges(); return(Redirect("/Cheese")); } return(View(addCheeseViewModel)); }
//Controller/Action/Id - default routing public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } CheeseCategory theCategory = context.Categories .Include(cat => cat.Cheeses) .Single(cat => cat.ID == id); /* alternative * * IList<Cheese> theCheeses = context.Cheeses * .Include(c => c.Category) * .Where(c => c.CategoryID == id) * .ToList(); * */ ViewBag.title = "Cheeses in category: " + theCategory.Name; return(View("Index", theCategory.Cheeses)); }
public IActionResult Edit(AddEditCheeseViewModel addEditCheeseViewModel) { if (ModelState.IsValid) { CheeseCategory newCheeseCategory = context.Categories.Single(c => c.ID == addEditCheeseViewModel.CategoryID); Cheese cheeseEdit = context.Cheeses.Single(c => c.ID == addEditCheeseViewModel.cheeseId); //CheeseData.GetByID(addEditCheeseViewModel.cheeseId); cheeseEdit.Name = addEditCheeseViewModel.Name; cheeseEdit.Description = addEditCheeseViewModel.Description; cheeseEdit.Category = newCheeseCategory; cheeseEdit.Rating = addEditCheeseViewModel.Rating; context.SaveChanges(); return(Redirect("/Cheese")); } return(View(new AddEditCheeseViewModel(context.Categories.ToList()))); }