//this method saves the logged hours to the volunteerhour table public IActionResult LogMyHours(VolunteerHour @volunteerhour) { //checks that the values are properly populated if (ModelState.IsValid) { //calls repository method to save event to the database TimeSpan numHours = @volunteerhour.EndTime - @volunteerhour.StartTime; double totalHours = numHours.TotalHours; @volunteerhour.NumberOfHours = Math.Round(totalHours, 2); repository.SaveVolunteerHour(@volunteerhour); //returns to the myLogged hours page to validate the hours were submitted EventRegistration dbEventReg = context.EventRegistrations .FirstOrDefault(p => p.EventID == volunteerhour.EventID && p.UserId == volunteerhour.UserId); if (dbEventReg != null) { dbEventReg.isLogged = true; context.SaveChanges(); } return(RedirectToAction("MyLoggedHours")); } else { // there is something wrong with the data values //this is where you could add validate error messages, or in your model. return(View(volunteerhour)); } }
//this method creates a new log my hours object request public ViewResult LogMyHoursCreate(int Id) { //creates new volunteer hour object var logHours = new VolunteerHour(); //creating the viewmodel var viewModel = new LogMyHoursViewModel() { //assignment our volunteerhour instance to a new volunteerhour object VolunteerHour = logHours, //gets an event instance from the constructor Event = eventRepository.Events .FirstOrDefault(a => a.EventID == Id), //provides a list of events for the user to select from Events = context.Events.ToList(), EventRegistration = registrationRepository.EventRegistrations .FirstOrDefault(a => a.EventID == Id), EventRegistrations = registrationRepository.EventRegistrations.ToList() }; //returns the new/blank viewmodel object to the view with data to populate the form with return(View("~/Views/VolunteerDonorDashboard/LogMyHours.cshtml", viewModel)); }
//this method is similar to above but also instantiates the user. You may be able to cut it to one //method, I think I did two just following some of our other code, this works fine though. public async Task <ViewResult> LogMyHours(string userId, int Id) { //retrieves user from the usermanager class var user = await userManager.FindByIdAsync(userId); //creates new volunteerHour object VolunteerHour logHours = new VolunteerHour(); //creates a viewmodel to give us data to populate our form var viewModel = new LogMyHoursViewModel() { VolunteerHour = logHours, Events = eventRepository.Events.ToList(), Event = eventRepository.Events .FirstOrDefault(a => a.EventID == Id), User = user, EventRegistration = registrationRepository.EventRegistrations .FirstOrDefault(a => a.EventID == Id), EventRegistrations = registrationRepository.EventRegistrations.ToList() }; //returns the viewmodel with the user-populated data return(View(viewModel)); }