public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } ElementKey = await _context.ElementKey .Include(e => e.ElementType).FirstOrDefaultAsync(m => m.ElementKeyID == id); if (ElementKey == null) { return(NotFound()); } return(Page()); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } ; ElementType elementType = _context.ElementTypes .Include(e => e.Keys) .AsNoTracking() .FirstOrDefault(e => e.ElementTypeID == ElementKey.ElementTypeID); if (elementType != null) { //проверка ключа на существование Estimator.Models.ElementKey keyExists = elementType.Keys .FirstOrDefault(c => PrepareStr(c.Key) == PrepareStr(ElementKey.Key)); if (keyExists != null) { if (keyExists.ElementKeyID != ElementKey.ElementKeyID) { ModelState.AddModelError("", "Ключ уже существует для данного типа элемента"); ElementKey.ElementType = elementType; return(Page()); } } keyExists = null; } if (ElementKey.ElementKeyID > 0) { // ключ существует _context.Attach(ElementKey).State = EntityState.Modified; } else { // новый ключ _context.ElementKey.Add(ElementKey); } await _context.SaveChangesAsync(); ElementKey.ElementType = elementType; return(RedirectToPage("./Index", new { id = elementType.ElementTypeID })); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } ElementKey = await _context.ElementKey.FindAsync(id); if (ElementKey != null) { _context.ElementKey.Remove(ElementKey); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index", new { id = ElementKey.ElementTypeID })); }