public async Task <IActionResult> Edit(int?id, byte[] rowVersion)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var customerToUpdate = await _context.Customers.SingleOrDefaultAsync(c => c.ID == id);

            if (customerToUpdate == null)
            {
                Customer deletedCustomer = new Customer();
                await TryUpdateModelAsync(deletedCustomer);

                ModelState.AddModelError(string.Empty, "Unable to save changes. The customer was deleted by another user.");
                return(View(deletedCustomer));
            }

            _context.Entry(customerToUpdate).Property("RowVersion").OriginalValue = rowVersion;

            if (await TryUpdateModelAsync <Customer>(customerToUpdate, "", c => c.CustomerName, c => c.Email))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (Customer)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save changes. The customer was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (Customer)databaseEntry.ToObject();

                        if (databaseValues.CustomerName != clientValues.CustomerName)
                        {
                            ModelState.AddModelError("CustomerName", $"Current customer: {databaseValues.CustomerName}");
                        }
                        if (databaseValues.Email != clientValues.Email)
                        {
                            ModelState.AddModelError("Email", $"Current email: {databaseValues.Email}");
                        }

                        ModelState.AddModelError("", "The record you attempted to edit " +
                                                 "was modified by another user after you got the original value.");
                        ModelState.AddModelError("", "The edit operation was canceled and the current valurs in the database " +
                                                 "have been displayed.");
                        ModelState.AddModelError("", "If you still want to edit this record, click the save button again. " +
                                                 "Otherwise click the Back to list to the previous page.");
                        customerToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
            }
            return(View(customerToUpdate));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int?id, string submit, string preUrl, byte[] rowVersion, Models.Task obj)
        {
            switch (submit)
            {
            case "Save As New Task":
            {
                IActionResult newTask = await Create(obj, preUrl);

                return(newTask);
            }
            }

            if (id == null)
            {
                return(NotFound());
            }

            var taskToUpdate = await _context.Tasks.SingleOrDefaultAsync(t => t.TaskID == id);

            if (taskToUpdate == null)
            {
                Models.Task deletedTask = new Models.Task();
                await TryUpdateModelAsync(deletedTask);

                ModelState.AddModelError(string.Empty, "Unable to save changes. This task was deleted by another user.");
                PopulateDropDownList(taskToUpdate.TaskID);
                return(View(deletedTask));
            }

            _context.Entry(taskToUpdate).Property("RowVersion").OriginalValue = rowVersion;

            if (await TryUpdateModelAsync(taskToUpdate, "", t => t.Date, t => t.SiteID, t => t.LeadingHandID, t => t.TaskStatus,
                                          t => t.Staff, t => t.Start, t => t.Finish, t => t.U, t => t.S, t => t.H,
                                          t => t.Vehicle, t => t.NumberOfStaff, t => t.WorkDescription,
                                          t => t.Progress, t => t.Quality, t => t.Returned, t => t.Comment))
            {
                try
                {
                    if (taskToUpdate.Finish - taskToUpdate.Start < new TimeSpan(1))
                    {
                        ModelState.AddModelError("", "Finish time must greater than start time.");
                    }
                    else
                    {
                        await _context.SaveChangesAsync();

                        return(Redirect(preUrl));
                    }
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValue    = (Models.Task)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save changes. The customer was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (Models.Task)databaseEntry.ToObject();

                        if (databaseValues.Date != clientValue.Date)
                        {
                            ModelState.AddModelError("Date", $"Current Date: {databaseValues.Date}");
                        }
                        if (databaseValues.LeadingHandID != clientValue.LeadingHandID)
                        {
                            ModelState.AddModelError("LeadingHandID", $"Current Leading Hand: {databaseValues.LeadingHand.FullName}");
                        }
                        if (databaseValues.WorkDescription != clientValue.WorkDescription)
                        {
                            ModelState.AddModelError("WorkDescription", $"Current Work Description: {databaseValues.WorkDescription}");
                        }
                        if (databaseValues.Staff != clientValue.Staff)
                        {
                            ModelState.AddModelError("Staff", $"Current Staff: {databaseValues.Staff}");
                        }
                        if (databaseValues.NumberOfStaff != clientValue.NumberOfStaff)
                        {
                            ModelState.AddModelError("NumberOfStaff", $"Current Number Of Staff: {databaseValues.NumberOfStaff}");
                        }
                        if (databaseValues.Progress != clientValue.Progress)
                        {
                            ModelState.AddModelError("Progress", $"Current Progress: {databaseValues.Progress}");
                        }
                        if (databaseValues.Start != clientValue.Start)
                        {
                            ModelState.AddModelError("Start", $"Current Start: {databaseValues.Start}");
                        }
                        if (databaseValues.Finish != clientValue.Finish)
                        {
                            ModelState.AddModelError("Finish", $"Current Finish: {databaseValues.Finish}");
                        }
                        if (databaseValues.U != clientValue.U)
                        {
                            ModelState.AddModelError("U", $"Current U: {databaseValues.U}");
                        }
                        if (databaseValues.S != clientValue.S)
                        {
                            ModelState.AddModelError("S", $"Current S: {databaseValues.S}");
                        }
                        if (databaseValues.H != clientValue.H)
                        {
                            ModelState.AddModelError("H", $"Current H: {databaseValues.H}");
                        }
                        if (databaseValues.Returned != clientValue.Returned)
                        {
                            ModelState.AddModelError("Returned", $"Current Returned: {databaseValues.Returned.ToString()}");
                        }
                        if (databaseValues.Quality != clientValue.Quality)
                        {
                            ModelState.AddModelError("Quality", $"Current Quality: {databaseValues.Quality.ToString()}");
                        }
                        if (databaseValues.TaskStatus != clientValue.TaskStatus)
                        {
                            ModelState.AddModelError("TaskStatus", $"Current TaskStatus: {databaseValues.TaskStatus.ToString()}");
                        }
                        if (databaseValues.Comment != clientValue.Comment)
                        {
                            ModelState.AddModelError("Comment", $"Current Comment: {databaseValues.Comment}");
                        }

                        ModelState.AddModelError("", "The record you attempted to edit " +
                                                 "was modified by another user after you got the original value.");
                        ModelState.AddModelError("", "The edit operation was canceled and the current valurs in the database " +
                                                 "have been displayed.");
                        ModelState.AddModelError("", "If you still want to edit this record, click the save button again. " +
                                                 "Otherwise click the Back to list to the previous page.");
                        taskToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
            }
            PopulateDropDownList(taskToUpdate.TaskID);
            return(View(taskToUpdate));
        }
        public async Task <IActionResult> Edit(int?id, string submit, byte[] rowVersion, DayWork obj, string preUrl)
        {
            switch (submit)
            {
            case "Save As New D/W":
            {
                IActionResult newDW = await Create(obj, preUrl);

                return(newDW);
            }
            }

            if (id == null)
            {
                return(NotFound());
            }

            var dayWorkToUpdate = await _context.DayWorks.SingleOrDefaultAsync(dw => dw.DayWorkID == id);

            if (dayWorkToUpdate == null)
            {
                DayWork deletedDayWork = new DayWork();
                await TryUpdateModelAsync(deletedDayWork);

                ModelState.AddModelError(string.Empty, "Unable to save changes. The Day Work you want to update wa deleted by another user.");
                return(View(deletedDayWork));
            }

            _context.Entry(dayWorkToUpdate).Property("RowVersion").OriginalValue = rowVersion;

            if (await TryUpdateModelAsync(dayWorkToUpdate, "", dw => dw.Date, dw => dw.Type,
                                          dw => dw.Description, dw => dw.Qty,
                                          dw => dw.Truck, dw => dw.Scaffolder, dw => dw.NumOfWorkers))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(Redirect(preUrl));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValue    = (DayWork)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save changes. This Day work was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (DayWork)databaseEntry.ToObject();

                        if (databaseValues.Date != clientValue.Date)
                        {
                            ModelState.AddModelError("Date", $"Current value: {databaseValues.Date}");
                        }
                        if (databaseValues.Type != clientValue.Type)
                        {
                            ModelState.AddModelError("Type", $"Current Type: {databaseValues.Type}");
                        }
                        if (databaseValues.Description != clientValue.Description)
                        {
                            ModelState.AddModelError("Description", $"Current Description: {databaseValues.Description}");
                        }
                        if (databaseValues.Qty != clientValue.Qty)
                        {
                            ModelState.AddModelError("Qty", $"Current Qty: {databaseValues.Qty}");
                        }
                        if (databaseValues.Truck != clientValue.Truck)
                        {
                            ModelState.AddModelError("Truck", $"Current Truck: {databaseValues.Truck}");
                        }
                        if (databaseValues.Scaffolder != clientValue.Scaffolder)
                        {
                            ModelState.AddModelError("Scaffolder", $"Current Scaffolder: {databaseValues.Scaffolder}");
                        }
                        if (databaseValues.NumOfWorkers != clientValue.NumOfWorkers)
                        {
                            ModelState.AddModelError("NumOfWorkers", $"Current NumOfWorkers: {databaseValues.NumOfWorkers}");
                        }

                        ModelState.AddModelError("", "The record you attempted to edit " +
                                                 "was modified by another user after you got the original value.");
                        ModelState.AddModelError("", "The edit operation was canceled and the current valurs in the database " +
                                                 "have been displayed.");
                        ModelState.AddModelError("", "If you still want to edit this record, click the save button again. " +
                                                 "Otherwise click the Back to list to the previous page.");
                        dayWorkToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
            }
            //ViewData["TaskID"] = new SelectList(_context.Tasks, "TaskID", "TaskID", dayWorkToUpdate.TaskID);
            PopulateDropDownList(dayWorkToUpdate.TaskID);
            return(View(dayWorkToUpdate));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> EditPost(int?id, byte[] rowVersion)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var projectToUpdate = await _context.Projects.SingleOrDefaultAsync(p => p.SiteID == id);

            if (projectToUpdate == null)
            {
                Project deletedProject = new Project();
                await TryUpdateModelAsync(deletedProject);

                PopulateDropDownList(projectToUpdate.CustomerID);
                ModelState.AddModelError(string.Empty, "Unable to save changes. The project was deleted by another user");
                return(View(deletedProject));
            }

            _context.Entry(projectToUpdate).Property("RowVersion").OriginalValue = rowVersion;

            if (await TryUpdateModelAsync <Project>(projectToUpdate, "", p => p.Site, p => p.SiteManager, p => p.Status, p => p.TowerManager, p => p.QS,
                                                    p => p.Address, p => p.CustomerID, p => p.Branch, p => p.Invoice, p => p.Quote))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (Project)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save changes. The customer was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (Project)databaseEntry.ToObject();

                        if (databaseValues.Site != clientValues.Site)
                        {
                            ModelState.AddModelError("Site", $"Current Site name: {databaseValues.Site}");
                        }
                        if (databaseValues.CustomerID != clientValues.CustomerID)
                        {
                            ModelState.AddModelError("CustomerID", $"Current customer: {databaseValues.Customer.CustomerName}");
                        }
                        if (databaseValues.Address != clientValues.Address)
                        {
                            ModelState.AddModelError("Address", $"Current Address: {databaseValues.Address}");
                        }
                        if (databaseValues.Branch != clientValues.Branch)
                        {
                            ModelState.AddModelError("Branch", $"Current Branch: {databaseValues.Branch.ToString()}");
                        }
                        if (databaseValues.TowerManager != clientValues.TowerManager)
                        {
                            ModelState.AddModelError("TowerManager", $"Current Tower Manager: {databaseValues.TowerManager}");
                        }
                        if (databaseValues.Status != clientValues.Status)
                        {
                            ModelState.AddModelError("Status", $"Current Status: {databaseValues.Status.ToString()}");
                        }
                        if (databaseValues.QS != clientValues.QS)
                        {
                            ModelState.AddModelError("QS", $"Current QS: {databaseValues.QS}");
                        }
                        if (databaseValues.SiteManager != clientValues.SiteManager)
                        {
                            ModelState.AddModelError("SiteManager", $"Current Site Manager: {databaseValues.SiteManager}");
                        }
                        if (databaseValues.Quote != clientValues.Quote)
                        {
                            ModelState.AddModelError("Quote", $"Current Quote: {databaseValues.Quote}");
                        }
                        if (databaseValues.Invoice != clientValues.Invoice)
                        {
                            ModelState.AddModelError("Invoice", $"Current Invoice: {databaseValues.Invoice}");
                        }

                        ModelState.AddModelError("", "The record you attempted to edit " +
                                                 "was modified by another user after you got the original value.");
                        ModelState.AddModelError("", "The edit operation was canceled and the current valurs in the database " +
                                                 "have been displayed.");
                        ModelState.AddModelError("", "If you still want to edit this record, click the save button again. " +
                                                 "Otherwise click the Back to list to the previous page.");
                        projectToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
            }
            PopulateDropDownList(projectToUpdate.CustomerID);
            return(View(projectToUpdate));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Edit(int?id, byte[] rowVersion)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var leadingHandToUpdate = await _context.LeadingHands.SingleOrDefaultAsync(m => m.ID == id);

            if (leadingHandToUpdate == null)
            {
                LeadingHand deletedLeadingHand = new LeadingHand();
                await TryUpdateModelAsync(deletedLeadingHand);

                ModelState.AddModelError(string.Empty, "Unable to save changes.The customer was deleted by another user.");
                return(View(deletedLeadingHand));
            }

            _context.Entry(leadingHandToUpdate).Property("RowVersion").OriginalValue = rowVersion;

            if (await TryUpdateModelAsync <LeadingHand>(
                    leadingHandToUpdate, "", lh => lh.FirstName, lh => lh.LastName, lh => lh.ScaffoldTicket, lh => lh.DriversLicence))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (LeadingHand)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save changes. This Leading Hand was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (LeadingHand)databaseEntry.ToObject();

                        if (databaseValues.FirstName != clientValues.FirstName)
                        {
                            ModelState.AddModelError("FirstName", $"Current First Name: {databaseValues.FirstName}");
                        }
                        if (databaseValues.LastName != clientValues.LastName)
                        {
                            ModelState.AddModelError("LastName", $"Current Last Name: {databaseValues.LastName}");
                        }
                        if (databaseValues.ScaffoldTicket != clientValues.ScaffoldTicket)
                        {
                            ModelState.AddModelError("ScaffoldTicket", $"Current ScaffoldTicket: {databaseValues.ScaffoldTicket}");
                        }
                        if (databaseValues.DriversLicence != clientValues.DriversLicence)
                        {
                            ModelState.AddModelError("DriversLicence", $"Current DriversLicence: {databaseValues.DriversLicence}");
                        }

                        ModelState.AddModelError("", "The record you attempted to edit " +
                                                 "was modified by another user after you got the original value.");
                        ModelState.AddModelError("", "The edit operation was canceled and the current valurs in the database " +
                                                 "have been displayed.");
                        ModelState.AddModelError("", "If you still want to edit this record, click the save button again. " +
                                                 "Otherwise click the Back to list to the previous page.");
                        leadingHandToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
            }
            return(View(leadingHandToUpdate));
        }