public ActionResult Create()
 {
     Category cat = new Category();
     cat.Index = db.Categories.Count() + 1;
     cat.ImageDisplayFlag = true;
     return View(cat);
 }
        public CategoryWithBindedVendors(Category category)
        {
            this.category = category;
            var bindedVendors = _context.Vendors.Where(x => x.Deleted != true).Join(_context.CategoryToVendorMaps, v => v.Id, cvm => cvm.VendorId,
                (v, cvm) => new { v, cvm.CategoryId, cvm.Index }).Where(c => c.CategoryId == this.category.Id).OrderBy(x => x.Index);

            this.BindedVendors = new List<Vendor>();
            bindedVendors.ToList().ForEach(x => this.BindedVendors.Add(x.v));

            this.VendorsCanBeBinded = new List<Vendor>();
            var vendorsAllowToBeBinded = _context.Vendors.Where(x => x.Deleted != true).ToList<Vendor>().Except(BindedVendors);
            vendorsAllowToBeBinded.ToList().ForEach(x => this.VendorsCanBeBinded.Add(x));
        }
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                category.ImageDisplayFlag = true;

                category.RiskRanges = new List<RiskRange>();
                category.RiskRanges.Add(new RiskRange(1));
                category.RiskRanges.Add(new RiskRange(2));
                category.RiskRanges.Add(new RiskRange(3));
                db.Categories.Add(category);

                Category entry = db.Entry(category).Entity;
                System.Data.Entity.DbSet<Category> dbset = db.Categories;

                db.SaveChanges();
                return RedirectToAction("Edit/" + category.Id);
            }

            return View(category);
        }
 public AdminCategory(Category c)
 {
     this.Id = c.Id;
     this.Weight = c.Weight;
     this.Text =_context.Categories.Where(x => x.Id == this.Id).First().Title;
 }
        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 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);
        }