Beispiel #1
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));
        }
Beispiel #2
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));
            }
        }