public async Task <IActionResult> Edit(int id, [Bind("Id,Boat_crew_name,Boat_crew_address,Boat_crew_phone,Boat_crew_logo,Boat_crew_allocation,Created_At,Updated_At")] Boat_crew boat_crew) { if (id != boat_crew.Id) { return(NotFound()); } if (ModelState.IsValid) { try { boat_crew.Updated_At = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim()); _context.Update(boat_crew); _context.Entry(boat_crew).Property(x => x.Created_At).IsModified = false; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Boat_crewExists(boat_crew.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully updated Boat Team Details")); } return(View(boat_crew)); }
public async Task <IActionResult> Create([Bind("Id,Member_name,Created_At,Updated_At")] Members members) { Boat_crew _Crew = new Boat_crew(); using (var dbContextTransaction = _context.Database.BeginTransaction()) { try { /* To return the allocate the team member to the available space from the database if it exists*/ //var team_data = await _context.Boat_Crews.FirstOrDefaultAsync(m => m.Id == 1); var team_data = await _context.Boat_crew_leader .Include(bc => bc.boat_Crew) .FirstOrDefaultAsync(m => m.User_Id == (string)TempData["User_Id"]); team_data.boat_Crew.Boat_crew_allocation -= 1; team_data.Updated_At = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim()); _context.Update(team_data); _context.Entry(team_data).Property(x => x.Created_At).IsModified = false; // To prevent the datetime property to be set as null on update operation _Crew.Id = team_data.boat_Crew.Id; await _context.SaveChangesAsync(); // Commit the transaction in the above number operations of the database context dbContextTransaction.Commit(); } catch (Exception e) { Console.WriteLine("Error: " + e); // In case of errors committed in the transaction. Changes will be rollback to the previous state dbContextTransaction.Rollback(); } } if (ModelState.IsValid) { var get_boat_crew = await _context.Boat_Crews.SingleOrDefaultAsync(x => x.Id == _Crew.Id); members.Boat_Crew = get_boat_crew; members.Created_At = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim()); _context.Add(members); await _context.SaveChangesAsync(); string userId = TempData["User_Id"].ToString(); return(RedirectToAction(nameof(Index), new { Id = userId }).WithSuccess("Success", "Successfully Registered member to the team")); } return(View(members)); }
public async Task <IActionResult> Create([Bind("Id,Boat_crew_name,Boat_crew_address,Boat_crew_phone,Boat_crew_logo,Boat_crew_allocation,Created_At,Updated_At")] Boat_crew boat_crew, List <IFormFile> Selected_files) { // For the image upload to work we needed to add enctype="multipart/form-data" in the form tag. if (Selected_files != null) { foreach (var boat_image in Selected_files) { if (boat_image.Length > 0) { var filename = Path.GetFileName(boat_image.FileName); // Get root path directory var rootpath = Path.Combine(_environment.WebRootPath, "Application_Files\\BoatTeamImages\\"); // To check if directory exists. If the directory does not exists we create a new directory if (!Directory.Exists(rootpath)) { Directory.CreateDirectory(rootpath); } // Get the path of filename var filepath = Path.Combine(_environment.WebRootPath, "Application_Files\\BoatTeamImages\\", filename); // Copy the image file to target directory path using (var stream = new FileStream(filepath, FileMode.Create)) { await boat_image.CopyToAsync(stream); } // To add the number of image filepath to the table model property before inserting to the database. boat_crew.Boat_crew_logo = "~/Application_Files/BoatTeamImages/" + filename; } } } if (ModelState.IsValid) { boat_crew.Created_At = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim()); _context.Add(boat_crew); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully Inserted Boat Team Details")); } return(View(boat_crew)); }