private void shiftIndexes(Question entry, int oldIndex, System.Data.Entity.DbSet <Question> dbset) { if (entry.Index - oldIndex == 1) { //Move forward by 1 step try { dbset.First(x => x.Index == entry.Index).Index = oldIndex; //If was last row - it will throw an exception } catch (Exception e) { } } else if (entry.Index > oldIndex) { //We moved forward foreach (var q in dbset.ToList()) { if (oldIndex < q.Index && q.Index < entry.Index) { q.Index = q.Index - 1; } } entry.Index = entry.Index - 1; } else if (entry.Index < oldIndex) { //We moved backwards foreach (var q in dbset.ToList()) { if (entry.Index <= q.Index && q.Index < oldIndex) { q.Index = q.Index + 1; } } } if (entry.Index > dbset.Count()) { entry.Index = dbset.Count(); } else if (entry.Index < 1) { entry.Index = 1; } }
public ActionResult DeleteConfirmed(int id) { Question q = _context.Questions.Find(id); System.Data.Entity.DbSet <Question> dbset = _context.Questions; int oldIndex = q.Index; q.Index = dbset.Count() + 100; shiftIndexes(q, oldIndex, dbset); _context.Questions.Remove(q); _context.SaveChanges(); return(RedirectToAction("Index")); }