public IActionResult Index(List <Unit> units) { // Find duplicates var duplicateName = units.GroupBy(x => x.Name).Where(g => g.Count() > 1) .Select(y => y.Key) .ToList(); var duplicateAbbr = units.GroupBy(x => x.Abbreviation).Where(g => g.Count() > 1) .Select(y => y.Key) .ToList(); var duplicateSortOrder = units.GroupBy(x => x.SortOrder).Where(g => g.Count() > 1) .Select(y => y.Key) .ToList(); // Find Sort Order <= 0 var badSort = units.Where(x => x.SortOrder <= 0); // Find usage // Error on duplicates if (duplicateName.Count > 0) { ModelState.AddModelError("", "Duplicate Name detected. Please assign every Unit a unique Name."); } if (duplicateAbbr.Count > 0) { ModelState.AddModelError("", "Duplicate Abbreviation detected. Please assign every Unit a unique Abbreviation."); } if (duplicateSortOrder.Count > 0) { ModelState.AddModelError("", "Duplicate Sort Order detected. Please assign every Unit a unique Sort Order."); } if (badSort.Count() > 0) { ModelState.AddModelError("", "Sort Order below 1 detected. Please assign every Unit a positive Sort Order."); } if (ModelState.IsValid) { for (int i = 0; i < units.Count(); i++) { _context.Update(units[i]); } _context.SaveChanges(); TempData["SuccessMessage"] = "Save Complete"; return(RedirectToAction(nameof(Index))); } return(View(units)); }
public IActionResult Index(List <CurrencyType> types) { if (ModelState.IsValid) { for (int i = 0; i < types.Count(); i++) { _context.Update(types[i]); } _context.SaveChanges(); TempData["SuccessMessage"] = "Save Complete"; return(RedirectToAction(nameof(Index))); } return(View(types)); }