public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Description,Created,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,OwnerUserId,DeveloperUserId")] Ticket ticket)
        {
            if (id != ticket.Id)
            {
                return(NotFound());
            }
            // Get snapshot of old ticket
            Ticket oldTicket = await _context.Tickets
                               .Include(t => t.TicketPriority)
                               .Include(t => t.TicketStatus)
                               .Include(t => t.TicketType)
                               .Include(t => t.DeveloperUser)
                               .Include(t => t.Project)
                               .Include(t => t.OwnerUser)
                               .AsNoTracking()
                               .FirstOrDefaultAsync(t => t.Id == ticket.Id);

            if (ModelState.IsValid)
            {
                try
                {
                    ticket.Updated = DateTime.Now;
                    _context.Update(ticket);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TicketExists(ticket.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                // Get snapshot of new ticket
                Ticket newTicket = await _context.Tickets
                                   .Include(t => t.TicketPriority)
                                   .Include(t => t.TicketStatus)
                                   .Include(t => t.TicketType)
                                   .Include(t => t.DeveloperUser)
                                   .Include(t => t.Project)
                                   .Include(t => t.OwnerUser)
                                   .AsNoTracking()
                                   .FirstOrDefaultAsync(t => t.Id == ticket.Id);

                // Get Current UserId
                string userId = _userManager.GetUserId(User);
                // Add History
                await _historyService.AddHistory(oldTicket, newTicket, userId);

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["DeveloperUserId"]  = new SelectList(_context.Users, "Id", "Id", ticket.DeveloperUserId);
            ViewData["OwnerUserId"]      = new SelectList(_context.Users, "Id", "Id", ticket.OwnerUserId);
            ViewData["ProjectId"]        = new SelectList(_context.Projects, "Id", "Name", ticket.ProjectId);
            ViewData["TicketPriorityId"] = new SelectList(_context.TicketPriorities, "Id", "Id", ticket.TicketPriorityId);
            ViewData["TicketStatusId"]   = new SelectList(_context.TicketStatuses, "Id", "Id", ticket.TicketStatusId);
            ViewData["TicketTypeId"]     = new SelectList(_context.TicketTypes, "Id", "Id", ticket.TicketTypeId);
            return(View(ticket));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Description,Created,Updated,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,OwnerUserId,DeveloperUserId")] Ticket ticket)
        {
            if (id != ticket.Id)
            {
                return(NotFound());
            }

            Ticket oldTicket = await _context.Tickets
                               //.Where(t => t.Id == ticket.Id)
                               //.Include(d => d.Description)
                               .Include(o => o.OwnerUser)
                               .Include(t => t.TicketPriority)
                               .Include(t => t.TicketStatus)
                               .Include(t => t.TicketType)
                               .Include(t => t.DeveloperUser)
                               .Include(t => t.Project)
                               .AsNoTracking()
                               .FirstOrDefaultAsync(t => t.Id == ticket.Id);


            if (!User.IsInRole("Demo"))
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        ticket.Updated = DateTimeOffset.Now;
                        _context.Update(ticket);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!TicketExists(ticket.Id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }

                    //Add history
                    string userId    = _userManager.GetUserId(User);
                    Ticket newTicket = await _context.Tickets
                                       .Include(t => t.TicketPriority)
                                       .Include(t => t.TicketStatus)
                                       .Include(t => t.TicketType)
                                       .Include(t => t.DeveloperUser)
                                       .Include(t => t.Project)
                                       .AsNoTracking()
                                       .FirstOrDefaultAsync(t => t.Id == ticket.Id);

                    await _historiesService.AddHistory(oldTicket, newTicket, userId);

                    ////return RedirectToAction(nameof(Index));
                }
                ViewData["DeveloperUserId"]  = new SelectList(_context.Users, "Id", "Id", ticket.DeveloperUserId);
                ViewData["OwnerUserId"]      = new SelectList(_context.Users, "Id", "Id", ticket.OwnerUserId);
                ViewData["ProjectId"]        = new SelectList(_context.Projects, "Id", "Name", ticket.ProjectId);
                ViewData["TicketPriorityId"] = new SelectList(_context.TicketPriorities, "Id", "Id", ticket.TicketPriorityId);
                ViewData["TicketStatusId"]   = new SelectList(_context.TicketStatuses, "Id", "Id", ticket.TicketStatusId);
                ViewData["TicketTypeId"]     = new SelectList(_context.TicketTypes, "Id", "Id", ticket.TicketTypeId);
                return(RedirectToAction("Details", "Tickets", new { id = ticket.Id }));
            }
            else
            {
                TempData["DemoLockout"] = "Your changes will not be saved.  To make changes to the database please log in as a full user.";
                return(RedirectToAction("Details", "Tickets", new { id = ticket.Id }));
            }
        }