// GET: Venues public ActionResult Index() { var model = new Euro2016.Models.VenuesViewModel(); //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { model.Venues = dbContext.Venues.ToList(); } return(View(model)); }
[Authorize] //this means the user has to login to access the view public ActionResult Edit(int id) { var model = new Euro2016.Models.VenuesViewModel(); //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var venue = dbContext.Venues.Where(x => x.Id == id).FirstOrDefault(); model.Name = venue.Name; } return(View(model)); }
[Authorize] //this means the user has to login to access the view public ActionResult Edit(Euro2016.Models.VenuesViewModel model) { if (!ModelState.IsValid) //this checks if the model is valid, if it's not valid then we are returning the view and displaying an error. The model is invalid if a required property is not filled in for example. { return(View(model)); } using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var venue = dbContext.Venues.Where(x => x.Id == model.Id).FirstOrDefault(); venue.Name = model.Name; dbContext.SaveChanges(); } return(RedirectToAction("Index")); }
[Authorize] //this means the user has to login to access the view public ActionResult Add(Euro2016.Models.VenuesViewModel model) { if (!ModelState.IsValid) //this checks if the model is valid, if it's not valid then we are returning the view and displaying an error. The model is invalid if a required property is not filled in for example. { return(View(model)); } var venue = new Venue(); //this is creating a new instance of an object venue.Name = model.Name; venue.CreationDate = DateTime.Now; venue.CreationSource = "Dan"; using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { dbContext.Venues.Add(venue); dbContext.SaveChanges(); } return(RedirectToAction("Index")); }