//Get request to get the page of the selected donation //URL: Donations/Update/1 public ActionResult Update(int id) { //find the donation in the db var donation = db.Donations.SingleOrDefault(d => d.Id == id); //set the viewModel for the update page: Display foreign keys(Provinces , Designations) var viewModel = new NewDonationViewModel { Donation = donation, Designations = db.Designations.ToList(), Provinces = db.Provinces.ToList() }; //display the update donation page return(View("Update", viewModel)); }
//Get the Add page: this only shows the Add page //with populated provinces and designations from other tables public ActionResult Add() { //to use the dbcontext we need to define it at the top of the controller //dbsets have been already added to the dbcontext file //we need a viewModel because this is a list from another table var provinces = db.Provinces.ToList(); var designations = db.Designations.ToList(); //initialize the viewmodel var viewModel = new NewDonationViewModel { Provinces = provinces, Designations = designations }; //pass the viewModel to the view return(View(viewModel)); }