Exemplo n.º 1
0
        public async Task <IActionResult> Update(int id)
        {
            var camperActivityToUpdate = await _context.CamperActivities
                                         .Include(c => c.Activity)
                                         .Include(c => c.Camper)
                                         .FirstOrDefaultAsync(c => c.ID == id);

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

            if (await TryUpdateModelAsync <CamperActivity>(camperActivityToUpdate, "",
                                                           p => p.NumberOfSessions, p => p.Fee, p => p.ActivityID))
            {
                try
                {
                    _context.Update(camperActivityToUpdate);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index", new { camperActivityToUpdate.CamperID }));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CamperActivityExists(camperActivityToUpdate.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (DbUpdateException dex)
                {
                    if (dex.InnerException.Message.ToUpper().Contains("UNIQUE"))
                    {
                        ModelState.AddModelError("ActivityID", "Unable to save changes. Remember, a camper canot be in an activity more then once.");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
                    }
                }
            }
            PopulateDropDownLists(camperActivityToUpdate);
            return(View(camperActivityToUpdate));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, string[] selectedOptions)
        {
            var compoundToUpdate = await _context.Compounds
                                   .Include(c => c.CounselorCompounds)
                                   .SingleOrDefaultAsync(d => d.ID == id);

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

            //Update the Compound's Counselors
            UpdateCounselorCompounds(selectedOptions, compoundToUpdate);

            if (await TryUpdateModelAsync <Compound>(compoundToUpdate, "",
                                                     p => p.Name))
            {
                try
                {
                    _context.Update(compoundToUpdate);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CompoundExists(compoundToUpdate.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
                }
            }
            //Validaiton Error so give the user another chance.
            PopulateAssignedCounselorData(compoundToUpdate);
            return(View(compoundToUpdate));
        }
        public async Task <IActionResult> Edit(int id)
        {
            var dietaryRestrictionToUpdate = await _context.DietaryRestrictions.FindAsync(id);

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

            if (await TryUpdateModelAsync <DietaryRestriction>(dietaryRestrictionToUpdate, "",
                                                               p => p.Name))
            {
                try
                {
                    _context.Update(dietaryRestrictionToUpdate);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DietaryRestrictionExists(dietaryRestrictionToUpdate.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
                }
            }
            return(View(dietaryRestrictionToUpdate));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(int id, Byte[] RowVersion)
        {
            var counselorToUpdate = await _context.Counselors.FindAsync(id);

            //Check that you got it or exit with a not found error
            if (counselorToUpdate == null)
            {
                return(NotFound());
            }

            if (await TryUpdateModelAsync <Counselor>(counselorToUpdate, "",
                                                      p => p.FirstName, p => p.MiddleName, p => p.LastName, p => p.Nickname,
                                                      p => p.SIN))
            {
                try
                {
                    //Put the original RowVersion value in the OriginalValues collection for the entity
                    _context.Entry(counselorToUpdate).Property("RowVersion").OriginalValue = RowVersion;
                    _context.Update(counselorToUpdate);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException ex)// Added for concurrency
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (Counselor)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError("",
                                                 "Unable to save changes. The Counselor was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (Counselor)databaseEntry.ToObject();
                        if (databaseValues.FirstName != clientValues.FirstName)
                        {
                            ModelState.AddModelError("FirstName", "Current value: "
                                                     + databaseValues.FirstName);
                        }
                        if (databaseValues.MiddleName != clientValues.MiddleName)
                        {
                            ModelState.AddModelError("MiddleName", "Current value: "
                                                     + databaseValues.MiddleName);
                        }
                        if (databaseValues.LastName != clientValues.LastName)
                        {
                            ModelState.AddModelError("LastName", "Current value: "
                                                     + databaseValues.LastName);
                        }
                        if (databaseValues.Nickname != clientValues.Nickname)
                        {
                            ModelState.AddModelError("Nickname", "Current value: "
                                                     + databaseValues.Nickname);
                        }
                        if (databaseValues.SIN != clientValues.SIN)
                        {
                            ModelState.AddModelError("SIN", "Current value: "
                                                     + databaseValues.SIN);
                        }
                        ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                                                 + "was modified by another user after you received your values. The "
                                                 + "edit operation was canceled and the current values in the database "
                                                 + "have been displayed. If you still want to save your version of this record, click "
                                                 + "the Save button again. Otherwise click the 'Back to List' hyperlink.");
                        counselorToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
                catch (DbUpdateException dex)
                {
                    if (dex.InnerException.Message.Contains("IX_Counselors_SIN"))
                    {
                        ModelState.AddModelError("SIN", "Unable to save changes. Remember, you cannot have duplicate SIN numbers.");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
                    }
                }
            }
            return(View(counselorToUpdate));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Edit(int id, string[] selectedOptions, Byte[] RowVersion, string chkRemoveImage, IFormFile thePicture)
        {
            var camperToUpdate = await _context.Campers
                                 .Include(c => c.Compound)
                                 .Include(c => c.Counselor)
                                 .Include(c => c.CamperDiets)
                                 .ThenInclude(cd => cd.DietaryRestriction)
                                 .FirstOrDefaultAsync(c => c.ID == id);

            //Check that you got it or exit with a not found error
            if (camperToUpdate == null)
            {
                return(NotFound());
            }

            //If Staff Role, check that they created this Camper
            //Note: They should never get here, but just in case...
            if (User.IsInRole("Staff"))
            {
                if (User.Identity.Name != camperToUpdate.CreatedBy)
                {
                    //Cannot edit this camper
                    ModelState.AddModelError("",
                                             "Edit Not Allowed. As Staff, you can only edit campers that you put into the system.");
                    PopulateDropDownLists(camperToUpdate);
                    PopulateAssignedDietaryRestrictionData(camperToUpdate);
                    return(View("EditStaff", camperToUpdate));
                }
            }

            //Update the medical history
            UpdateCamperDietaryRestrictions(selectedOptions, camperToUpdate);

            if (await TryUpdateModelAsync <Camper>(camperToUpdate, "",
                                                   p => p.FirstName, p => p.MiddleName, p => p.LastName, p => p.DOB, p => p.Gender,
                                                   p => p.CompoundID, p => p.Phone, p => p.eMail, p => p.CounselorID))
            {
                try
                {
                    //For the image
                    if (chkRemoveImage != null)
                    {
                        camperToUpdate.imageContent  = null;
                        camperToUpdate.imageMimeType = null;
                        camperToUpdate.imageFileName = null;
                    }
                    else
                    {
                        if (thePicture != null)
                        {
                            string mimeType   = thePicture.ContentType;
                            long   fileLength = thePicture.Length;
                            if (!(mimeType == "" || fileLength == 0))//Looks like we have a file!!!
                            {
                                if (mimeType.Contains("image"))
                                {
                                    using (var memoryStream = new MemoryStream())
                                    {
                                        await thePicture.CopyToAsync(memoryStream);

                                        camperToUpdate.imageContent = memoryStream.ToArray();
                                    }
                                    camperToUpdate.imageMimeType = mimeType;
                                    camperToUpdate.imageFileName = thePicture.FileName;
                                }
                            }
                        }
                    }
                    //Put the original RowVersion value in the OriginalValues collection for the entity
                    _context.Entry(camperToUpdate).Property("RowVersion").OriginalValue = RowVersion;
                    _context.Update(camperToUpdate);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (RetryLimitExceededException /* dex */)
                {
                    ModelState.AddModelError("", "Unable to save changes after multiple attempts. Try again, and if the problem persists, see your system administrator.");
                }
                catch (DbUpdateConcurrencyException ex)// Added for concurrency
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (Camper)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError("",
                                                 "Unable to save changes. The Camper was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (Camper)databaseEntry.ToObject();
                        if (databaseValues.FirstName != clientValues.FirstName)
                        {
                            ModelState.AddModelError("FirstName", "Current value: "
                                                     + databaseValues.FirstName);
                        }
                        if (databaseValues.MiddleName != clientValues.MiddleName)
                        {
                            ModelState.AddModelError("MiddleName", "Current value: "
                                                     + databaseValues.MiddleName);
                        }
                        if (databaseValues.LastName != clientValues.LastName)
                        {
                            ModelState.AddModelError("LastName", "Current value: "
                                                     + databaseValues.LastName);
                        }
                        if (databaseValues.DOB != clientValues.DOB)
                        {
                            ModelState.AddModelError("DOB", "Current value: "
                                                     + String.Format("{0:d}", databaseValues.DOB));
                        }
                        if (databaseValues.Phone != clientValues.Phone)
                        {
                            ModelState.AddModelError("Phone", "Current value: "
                                                     + String.Format("{0:(###) ###-####}", databaseValues.Phone));
                        }
                        if (databaseValues.eMail != clientValues.eMail)
                        {
                            ModelState.AddModelError("eMail", "Current value: "
                                                     + databaseValues.eMail);
                        }
                        if (databaseValues.Gender != clientValues.Gender)
                        {
                            ModelState.AddModelError("Gender", "Current value: "
                                                     + databaseValues.Gender);
                        }
                        //For the foreign key, we need to go to the database to get the information to show
                        if (databaseValues.CompoundID != clientValues.CompoundID)
                        {
                            Compound databaseCompound = await _context.Compounds.SingleOrDefaultAsync(i => i.ID == databaseValues.CompoundID);

                            ModelState.AddModelError("CompoundID", $"Current value: {databaseCompound?.Name}");
                        }
                        //A little extra work for the nullable foreign key.  No sense going to the database and asking for something
                        //we already know is not there.
                        if (databaseValues.CounselorID != clientValues.CounselorID)
                        {
                            if (databaseValues.CounselorID.HasValue)
                            {
                                Counselor databaseCounselor = await _context.Counselors.SingleOrDefaultAsync(i => i.ID == databaseValues.CounselorID);

                                ModelState.AddModelError("CounselorID", $"Current value: {databaseCounselor?.FullName}");
                            }
                            else
                            {
                                ModelState.AddModelError("CounselorID", $"Current value: None");
                            }
                        }
                        ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                                                 + "was modified by another user after you received your values. The "
                                                 + "edit operation was canceled and the current values in the database "
                                                 + "have been displayed. If you still want to save your version of this record, click "
                                                 + "the Save button again. Otherwise click the 'Back to List' hyperlink.");
                        camperToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
                catch (DbUpdateException dex)
                {
                    if (dex.InnerException.Message.Contains("IX_Campers_eMail"))
                    {
                        ModelState.AddModelError("eMail", "Unable to save changes. Remember, you cannot have duplicate eMail addresses.");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
                    }
                }
            }
            PopulateDropDownLists(camperToUpdate);
            PopulateAssignedDietaryRestrictionData(camperToUpdate);

            if (User.IsInRole("Staff"))//Decide which version of the view to send
            {
                return(View("EditStaff", camperToUpdate));
            }
            else
            {
                return(View(camperToUpdate));
            }
        }