public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] Project project, string oldName) { if (id != project.Id) { return(NotFound()); } if (ModelState.IsValid) { try { Parts = await _context.Part.AsNoTracking().ToListAsync(); if (Parts != null) { for (int i = 0; i < Parts.Count; i++) { if (Parts[i].UsedFor == oldName) { Parts[i].UsedFor = project.Name; _context.Update(Parts[i]); } } } _context.Update(project); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProjectExists(project.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(project)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Now,UsedFor,Flagged,Color,TextColor")] Part part) { if (id != part.Id) { return(NotFound()); } if (ModelState.IsValid) { try { Projects = await _context.Project.AsNoTracking().ToListAsync(); var inThere = false; for (int i = 0; i < Projects.Count; i++) { if (Projects[i].Name == part.UsedFor) { inThere = true; } } if (inThere == false) { ViewData["Message"] = $"Project {part.UsedFor} is not a current project"; return(View(part)); } else { _context.Update(part); await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (!PartExists(part.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(part)); }