public async Task <IActionResult> Create([Bind("Id,Name,UserId,Tractor,Date,DriversLicense,DotCard,ExtraLogBook,PitCard,Tablet,DvirBook,Registration,InsuranceCard,Ifta,LogBook,EldGuide,PermitBook,PostAccidentKit,BlankAccidentReport,OregonFuelPermit,DriverSignature")] DotAudit dotAudit) { if (ModelState.IsValid) { //Grabbing logged in users id so when they post the data goes into their account string userId = _userManager.GetUserId(HttpContext.User); ApplicationUser user = _context.Users.Where(a => a.Id == userId).FirstOrDefault(); // get the user's most recent trip, sorted by the trip start date var mostRecentTrip = _context.Trip.Where(t => t.UserId == userId).OrderByDescending(x => x.StartTime).FirstOrDefault(); if (mostRecentTrip == null) { // user loaded page but doesn't have any trips in their history return(View()); } // Setting tractor number to the number that was used from that specific trip dotAudit.Tractor = mostRecentTrip.Tractor; dotAudit.UserId = user.Id; //Setting the name of the logged in user into the DotAudit form dotAudit.Name = user.FirstName + " " + user.LastName; _context.Add(dotAudit); await _context.SaveChangesAsync(); return(Redirect("~/EndRouteCheck/Create")); } return(View(dotAudit)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,UserId,Tractor,Date,DriversLicense,DotCard,ExtraLogBook,PitCard,Tablet,DvirBook,Registration,InsuranceCard,Ifta,LogBook,EldGuide,PermitBook,PostAccidentKit,BlankAccidentReport,OregonFuelPermit,DriverSignature")] DotAudit dotAudit) { if (id != dotAudit.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(dotAudit); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DotAuditExists(dotAudit.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(dotAudit)); }