public IActionResult GenerateParkedVehicles(int noParkedVehicles = 5) { Random rnd = new Random(); var existingRegNumbers = _context.ParkedVehicle.Select(pv => pv.RegNo).ToList(); var existingMembers = _context.Member.ToList(); var existingVehicleTypes = _context.VehicleType.ToList(); if (!existingMembers.Any()) { return(RedirectToAction(nameof(Index))); } for (int i = 0; i < noParkedVehicles; i++) { var generatedVehicle = _vehicleGenerator.GenerateVehicle(); if (existingRegNumbers.IndexOf(generatedVehicle.RegNo) == -1) { existingRegNumbers.Add(generatedVehicle.RegNo); generatedVehicle.Member = existingMembers.ElementAt(rnd.Next(existingMembers.Count() - 1)); if (existingVehicleTypes.Any()) { generatedVehicle.VehicleType = existingVehicleTypes.ElementAt(rnd.Next(existingVehicleTypes.Count() - 1)); } _context.Add(generatedVehicle); } } _context.SaveChanges(); return(RedirectToAction(nameof(Index))); }
public IActionResult AddOrEdit(MemberAddOrEditViewModel viewModel) { if (ModelState.IsValid) { var member = _mapper.Map <Member>(viewModel); //Create if (viewModel.Id == 0) { _context.Add(member); } else { _context.Update(member); } _context.SaveChanges(); return(RedirectToAction(nameof(Index))); } return(View(viewModel)); }