public void ValidatePlace(StoringPlace storingPlace, ModelStateDictionary ms) { if (_context.StoringPlaces.Any(p => p.Place == storingPlace.Place)) { ms.AddModelError("Place", "Дане місце не є унікальним"); ms["Place"].ValidationState = ModelValidationState.Invalid; } }
public async Task OnPostCreate([Bind("Place")] StoringPlace storingPlace) { ValidatePlace(storingPlace, ModelState); if (ModelState.IsValid) { _context.StoringPlaces.Add(storingPlace); await _context.SaveChangesAsync(); } Places = await _context.StoringPlaces.ToListAsync(); }
public async Task OnPostEdit([Bind("Id, Place")] StoringPlace storingPlace) { var oldPlace = _context.StoringPlaces.AsNoTracking().SingleOrDefault(p => p.Id == storingPlace.Id); if (ModelState.IsValid) { if (!storingPlace.Equals(oldPlace) && !_context.StoringPlaces.Any(p => p.Place == storingPlace.Place && p.Id != storingPlace.Id)) { oldPlace = storingPlace; _context.StoringPlaces.Update(oldPlace); await _context.SaveChangesAsync(); } else { ModelState.AddModelError("Place", "Дане місце не є унікальним"); ModelState["Place"].ValidationState = ModelValidationState.Invalid; } } Places = await _context.StoringPlaces.ToListAsync(); }