//method that retrieves the users logged hours public async Task <IActionResult> MyLoggedHours(int ID, string userId) { var user = await userManager.FindByIdAsync(userId); //creates a new viewmodel object var viewModel = new LogMyHoursViewModel { //gets a lists of volunteerHours VolunteerHours = context.VolunteerHours.ToList(), //gets a list of events Events = context.Events.ToList(), //gets all volunteer hours, includes the events based on //volunteerHourID. VolunteerHour = context.VolunteerHours .Include(i => i.Event) .FirstOrDefault(x => x.VolunteerHourID == ID), //sets the UserId in the volunteerHour model to the userId passed //in to the method, so that you do not retrieve all volunteer hours //you may need to set this up to be passed to this method from your view, //I did not check that part. UserId = userId }; //returns viewModel of data to retrieve from the view. return(View(viewModel)); }
//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)); }