public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { //Add new cheese to existing cheeses if (ModelState.IsValid) { Cheese newCheese = AddCheeseViewModel.CreateCheese( addCheeseViewModel.Name, addCheeseViewModel.Description, addCheeseViewModel.Type, addCheeseViewModel.Rating); CheeseData.AddCheese(newCheese); return(Redirect("/Cheese")); } /* * Regex rgx = new Regex(@"^[A-Za-z ]+$"); * if (addCheeseViewModel.Name == null || rgx.IsMatch(addCheeseViewModel.Name) == false) * { * error = "Invalid Name"; * return Redirect("/Cheese/Add"); * }*/ 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 //}; Cheese newCheese = addCheeseViewModel.CreateCheese(); newCheese.Category = newCheeseCategory; //context.Cheeses.Add(newCheese); //context.SaveChanges(); context.Cheeses.Add(newCheese); context.SaveChanges(); return(Redirect("/Cheese")); } AddCheeseViewModel newCheeseViewModel = new AddCheeseViewModel(context.Categories.ToList()); return(View(newCheeseViewModel)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { Cheese newCheese = addCheeseViewModel.CreateCheese(); CheeseData.Add(newCheese); return(Redirect("/Cheese")); } return(View(addCheeseViewModel)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { context.Cheeses.Add(addCheeseViewModel.CreateCheese(addCheeseViewModel)); context.SaveChanges(); return(Redirect("/Cheese")); } //TODO Create method in AddCheeseViewModel that return an object of new cheese //and add this object to CheeseData CheeseData.Add(newCheese); return(View(addCheeseViewModel)); }
// Model binding: you can pass form data through to an object's constructor and if the input names match the the parameters in the constructor, the framework will automatically create a new object of that type. I prefer the explicit workflow as it allows me to visually track what parameters are being passed, stage by stage. public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { Cheese newCheese = addCheeseViewModel.CreateCheese(addCheeseViewModel); //add the new cheese to my existing cheeses CheeseData.Add(newCheese); return(Redirect("/cheese")); } return(View(addCheeseViewModel)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { Cheese newCheese = addCheeseViewModel.CreateCheese(); context.Cheeses.Add(newCheese); context.SaveChanges(); return(Redirect("/Cheese")); } return(View(addCheeseViewModel)); }
public IActionResult NewCheese(AddCheeseViewModel viewModel) { if (!ModelState.IsValid) { return(View("Add", viewModel)); } // add new cheese to static class list Cheese newCheese = viewModel.CreateCheese(); CheeseData.Add(newCheese); // go back to the list of cheeses return(Redirect("/cheese")); }
public IActionResult NewCheese(AddCheeseViewModel viewModel) { if (!ModelState.IsValid) { return(View("Add", viewModel)); } // add new cheese to static class list Cheese newCheese = viewModel.CreateCheese(context.Categories.ToList()); context.Cheeses.Add(newCheese); context.SaveChanges(); // go back to the list of cheeses return(Redirect("/cheese")); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { // Add the new cheese to my existing cheeses CheeseCategory newCheeseCategory = context.Categories.Single(c => c.ID == addCheeseViewModel.CategoryID); Cheese newCheese = AddCheeseViewModel.CreateCheese(addCheeseViewModel, newCheeseCategory); context.Cheeses.Add(newCheese); context.SaveChanges(); return(Redirect("/")); } return(View(addCheeseViewModel)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { // Add the new cheese to my existing cheeses //CheeseData.Add(new Cheese{ // Name = addCheeseViewModel.Name, // Description = addCheeseViewModel.Description, // Type = addCheeseViewModel.Type, // Rating = addCheeseViewModel.Rating //}); /*context.Cheeses.Add(addCheeseViewModel.CreateCheese()); * context.SaveChanges();*/ CheeseData.Add(addCheeseViewModel.CreateCheese()); return(Redirect("/")); } return(View(addCheeseViewModel)); }