public IActionResult EditCancel(InventoryAreaEditViewModel model)
 {
     //redirect back to details page for inventory area
     return(RedirectToAction("Details", new { id = model.InventoryAreaId }));
 }
        public async Task <IActionResult> Edit(int?id, int?displayCount)
        {
            if (id == null)
            {
                return(NotFound());
            }

            //get area from context
            var area = await(from a in _context.InventoryAreas.Include("ItemSlots.WildeRoverItem")
                             where a.InventoryAreaId == id
                             select a).SingleOrDefaultAsync();

            if (area == null)
            {
                return(NotFound());               //validate
            }
            //get WildeRoverItems to populate selectlist
            var items = await(from i in _context.WildeRoverItem
                              orderby i.ItemHouse, i.Name
                              select i).ToListAsync();

            //order InventorySlots in area by their number
            var slots = area.ItemSlots.OrderBy(t => t.Slot).ToList();

            //Create selection list
            //List<SelectListItem> selectList = new List<SelectListItem>();
            //selectList.Add
            //(
            //    //Selection if nothing is selected
            //    new SelectListItem
            //    {
            //        Value = 0.ToString(),
            //        Text = "<Select Item>"
            //    }
            //);
            ////Create selections for every WildeRoverItem
            //foreach (var item in items)
            //{
            //    selectList.Add
            //    (
            //        new SelectListItem
            //        {
            //            Value = item.WildeRoverItemId.ToString(),
            //            Text = item.Name
            //        }
            //    );
            //}

            //Create view model
            InventoryAreaEditViewModel model = new InventoryAreaEditViewModel();

            model.InventoryAreaId = area.InventoryAreaId;
            model.InventoryArea   = area;
            //model.ItemList = selectList;  //assign select list

            //Populate Slots
            model.SlotList = new List <InventoryAreaEditEntry>();

            if (displayCount == null)
            {
                displayCount = slots.Count;
            }

            for (int i = 0; i < displayCount; i++)
            {
                InventoryAreaEditEntry entry = new InventoryAreaEditEntry();
                entry.InventoryAreaId = area.InventoryAreaId;
                if (i < slots.Count)
                {
                    entry.InventorySlotId = slots[i].InventorySlotId;
                    entry.Slot            = slots[i].Slot;

                    if (slots[i].WildeRoverItemId == null)
                    {
                        entry.WildeRoverItemId = 0;
                    }
                    else
                    {
                        entry.WildeRoverItemId = (int)slots[i].WildeRoverItemId;
                    }
                }
                else
                {
                    entry.WildeRoverItemId = 0;
                    entry.Slot             = i + 1;
                }

                model.SlotList.Add(entry);
            }

            model.SlotDisplayCount = (int)displayCount;

            return(View(model));
        }
        public async Task <IActionResult> EditSave(InventoryAreaEditViewModel model)
        {
            if (model == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //Get area from context
                    var area = await(from a in _context.InventoryAreas.Include("ItemSlots.WildeRoverItem")
                                     where a.InventoryAreaId == model.InventoryAreaId
                                     select a).SingleOrDefaultAsync();

                    if (area == null)
                    {
                        return(NotFound());               //Validate
                    }
                    //Create dictionary of ItemSlots for O(1) updates
                    var slotDict = area.ItemSlots.ToDictionary(t => t.InventorySlotId);

                    //Loop through model.ItemSlots and update context
                    int i = 0;
                    while (i < model.SlotDisplayCount || i < area.ItemSlots.Count)
                    {
                        if (i < area.ItemSlots.Count && i < model.SlotDisplayCount)  //Update
                        {
                            var temp = slotDict[model.SlotList[i].InventorySlotId];
                            if (model.SlotList[i].WildeRoverItemId == 0)
                            {
                                temp.WildeRoverItemId = null;
                            }
                            else
                            {
                                temp.WildeRoverItemId = model.SlotList[i].WildeRoverItemId;
                            }

                            _context.Slots.Update(temp);
                        }
                        else if (i < model.SlotDisplayCount && i >= area.ItemSlots.Count)  //Add
                        {
                            InventorySlot newSlot = new InventorySlot();
                            newSlot.InventoryAreaId = area.InventoryAreaId;
                            newSlot.Slot            = i + 1;
                            if (model.SlotList[i].WildeRoverItemId != 0)
                            {
                                newSlot.WildeRoverItemId = model.SlotList[i].WildeRoverItemId;
                            }

                            _context.Slots.Add(newSlot);
                        }
                        else if (i >= model.SlotDisplayCount && i < area.ItemSlots.Count)  //Remove
                        {
                            var toRemove = area.ItemSlots[i];
                            area.ItemSlots.RemoveAt(i);

                            _context.Slots.Remove(toRemove);
                        }

                        i++;
                    }

                    await _context.SaveChangesAsync();  //Save context

                    return(RedirectToAction("Details", new { id = model.InventoryAreaId }));
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }

            return(View(model));
        }