public ActionResult Edit(Category category) { if (ModelState.IsValid) { Category entry = db.Entry(category).Entity; System.Data.Entity.DbSet <Category> dbset = db.Categories; //If we changed index, we need to shift other elements int oldIndex = dbset.First(x => x.Id == entry.Id).Index; shiftIndexes(entry, oldIndex, dbset); //Ugly workaround Category dbQ = dbset.First(x => x.Id == entry.Id); dbQ.Index = entry.Index; dbQ.Annotation = entry.Annotation; dbQ.Weight = entry.Weight; dbQ.Title = entry.Title; dbQ.ImageDisplayFlag = entry.ImageDisplayFlag; db.SaveChanges(); //db.Entry(category).State = EntityState.Modified; //db.SaveChanges(); return(RedirectToAction("Index")); } return(View(category)); }
private void shiftIndexes(Category entry, int oldIndex, System.Data.Entity.DbSet <Category> 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 TEntity GetFirst(Func <TEntity, bool> predicate) { return(_entitySet.First <TEntity>(predicate)); }