Example #1
0
        public async Task <IActionResult> Create([Bind("Id,UserName,Email")] Member member)
        {
            await CheckUnique(member);

            if (ModelState.IsValid)
            {
                _context.Add(member);
                await _context.SaveChangesAsync();

                TempData["Message"] = $"Member \'{member.UserName}\' is registered";

                return(RedirectToAction(nameof(Index)));
            }
            return(View(member));
        }
Example #2
0
        public async Task <IActionResult> Create([Bind("Id,Name")] VehicleType vehicleType)
        {
            if (await _context.VehicleType.AnyAsync(v => v.Name == vehicleType.Name))
            {
                ModelState.AddModelError("Name", $"\'{vehicleType.Name}\' already exists!");
            }

            if (ModelState.IsValid)
            {
                _context.Add(vehicleType);
                await _context.SaveChangesAsync();

                TempData["Message"] = $"Vehicle Type \'{vehicleType.Name}\' is created";

                return(RedirectToAction(nameof(Index)));
            }
            return(View(vehicleType));
        }
Example #3
0
        public async Task <IActionResult> Create2([Bind("RegNum,Color,UserName,TypeName")] CreatePVViewModel createPVViewModel)
        {
            //if (await _context.ParkedVehicle.AnyAsync(p => p.RegNum == createPVViewModel.RegNum))
            //{
            //    ModelState.AddModelError("RegNum", $"\'{createPVViewModel.RegNum}\' already exists!");
            //}

            //if (!await _context.Member.AnyAsync(m => m.UserName == createPVViewModel.UserName))
            //{
            //    ModelState.AddModelError("UserName", $"\'{createPVViewModel.UserName}\' do not exists!");
            //}

            //if (!await _context.VehicleType.AnyAsync(v => v.Name == createPVViewModel.TypeName))
            //{
            //    ModelState.AddModelError("TypeName", $"\'{createPVViewModel.TypeName}\' do not exists!");
            //}

            Member member = await _context.Member
                            .FirstOrDefaultAsync(m => m.UserName == createPVViewModel.UserName);

            if (member == null)
            {
                return(NotFound());
            }

            VehicleType vehicleType = await _context.VehicleType
                                      .FirstOrDefaultAsync(v => v.Name == createPVViewModel.TypeName);

            if (vehicleType == null)
            {
                return(NotFound());
            }

            DateTime      now           = DateTime.Now;
            ParkedVehicle parkedVehicle = new ParkedVehicle
            {
                RegNum        = createPVViewModel.RegNum,
                Color         = createPVViewModel.Color,
                CheckInTime   = now,
                MemberId      = member.Id,
                Member        = member,
                VehicleTypeId = vehicleType.Id,
                VehicleType   = vehicleType
            };

            //var serviceProvider = _context.GetService<IServiceProvider>();
            //var items = new Dictionary<object, object>();
            //var context = new ValidationContext(parkedVehicle, _serviceProvider, items);
            //var validationResults = new List<ValidationResult>();

            //if (!Validator.TryValidateObject(parkedVehicle, context, validationResults, true))
            //{
            //    foreach (var validationResult in validationResults)
            //    {
            //        if (validationResult.MemberNames.Count() > 0)
            //            ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
            //    }
            //}

            // DataAnnotationsValidator.TryValidateModel(parkedVehicle, this, null);
            DataAnnotationsValidator.TryValidateModel(parkedVehicle, this, _serviceProvider);

            //if (!DataAnnotationsValidator.TryValidate(parkedVehicle, out ICollection<ValidationResult> validationResults))
            //{
            //    foreach (var validationResult in validationResults)
            //    {
            //        if (validationResult.MemberNames.Count() > 0)
            //            ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
            //    }
            //}

            // see https://odetocode.com/Blogs/scott/archive/2011/06/29/manual-validation-with-data-annotations.aspx

            //public class DataAnnotationsValidator
            //{
            //    public bool TryValidate(object @object, out ICollection<ValidationResult> results)
            //    {
            //        var context = new ValidationContext(@object, serviceProvider: null, items: null);
            //        results = new List<ValidationResult>();
            //        return Validator.TryValidateObject(
            //            @object, context, results,
            //            validateAllProperties: true
            //        );
            //    }
            //}


            if (ModelState.IsValid)
            {
                _context.Add(parkedVehicle);
                await _context.SaveChangesAsync();

                TempData["Message"] = $"Vehicle \'{parkedVehicle.RegNum}\' is checked in";

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["UserName"] = new SelectList(_context.Member
                                                  .OrderBy(m => m.UserName), "UserName", "UserName");

            ViewData["TypeName"] = new SelectList(_context.VehicleType
                                                  .OrderBy(v => v.Name), "Name", "Name");

            return(View(createPVViewModel));
        }
        public async Task <IActionResult> Create2([Bind("RegNum,Color,UserName,TypeName")] CreatePVViewModel createPVViewModel)
        {
            if (await _context.ParkedVehicle.AnyAsync(p => p.RegNum == createPVViewModel.RegNum))
            {
                ModelState.AddModelError("RegNum", $"\'{createPVViewModel.RegNum}\' already exists!");
            }

            if (!await _context.Member.AnyAsync(m => m.UserName == createPVViewModel.UserName))
            {
                ModelState.AddModelError("UserName", $"\'{createPVViewModel.UserName}\' do not exists!");
            }

            if (!await _context.VehicleType.AnyAsync(v => v.Name == createPVViewModel.TypeName))
            {
                ModelState.AddModelError("TypeName", $"\'{createPVViewModel.TypeName}\' do not exists!");
            }

            if (ModelState.IsValid)
            {
                Member member = await _context.Member
                                .FirstOrDefaultAsync(m => m.UserName == createPVViewModel.UserName);

                if (member == null)
                {
                    return(NotFound());
                }

                VehicleType vehicleType = await _context.VehicleType
                                          .FirstOrDefaultAsync(v => v.Name == createPVViewModel.TypeName);

                if (vehicleType == null)
                {
                    return(NotFound());
                }

                DateTime      now           = DateTime.Now;
                ParkedVehicle parkedVehicle = new ParkedVehicle
                {
                    RegNum        = createPVViewModel.RegNum,
                    Color         = createPVViewModel.Color,
                    CheckInTime   = now,
                    MemberId      = member.Id,
                    Member        = member,
                    VehicleTypeId = vehicleType.Id,
                    VehicleType   = vehicleType
                };

                _context.Add(parkedVehicle);
                await _context.SaveChangesAsync();

                TempData["Message"] = $"Vehicle \'{parkedVehicle.RegNum}\' is checked in";

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["UserName"] = new SelectList(_context.Member
                                                  .OrderBy(m => m.UserName), "UserName", "UserName");

            ViewData["TypeName"] = new SelectList(_context.VehicleType
                                                  .OrderBy(v => v.Name), "Name", "Name");

            return(View(createPVViewModel));
        }