コード例 #1
0
        public async Task <IActionResult> Create([Bind("ID,FirstName,MiddleName,LastName,Nickname,SIN")] Counselor counselor)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Add(counselor);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            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(counselor));
        }
コード例 #2
0
        public async Task <IActionResult> Add([Bind("ID,Fee,NumberOfSessions,CamperID,ActivityID")] CamperActivity camperActivity, string CamperName)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Add(camperActivity);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index", new { camperActivity.CamperID }));
                }
            }
            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(camperActivity);
            ViewData["CamperName"] = CamperName;
            return(View(camperActivity));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("ID,Name")] DietaryRestriction dietaryRestriction)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Add(dietaryRestriction);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return(View(dietaryRestriction));
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("ID,Name")] Compound compound, string[] selectedOptions)
        {
            try
            {
                UpdateCounselorCompounds(selectedOptions, compound);
                if (ModelState.IsValid)
                {
                    _context.Add(compound);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            PopulateAssignedCounselorData(compound);
            return(View(compound));
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("ID,FirstName,MiddleName,LastName,DOB,Gender,eMail,Phone,CounselorID,CompoundID")]
                                                 Camper camper, string[] selectedOptions, IFormFile thePicture)
        {
            try
            {
                //Add the selected
                if (selectedOptions != null)
                {
                    camper.CamperDiets = new List <CamperDiet>();
                    foreach (var r in selectedOptions)
                    {
                        var condToAdd = new CamperDiet {
                            CamperID = camper.ID, DietaryRestrictionID = int.Parse(r)
                        };
                        camper.CamperDiets.Add(condToAdd);
                    }
                }
                if (ModelState.IsValid)
                {
                    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);

                                    camper.imageContent = memoryStream.ToArray();
                                }
                                camper.imageMimeType = mimeType;
                                camper.imageFileName = thePicture.FileName;
                            }
                        }
                    }
                    _context.Add(camper);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index", "CamperActivities", new { CamperID = camper.ID }));
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                ModelState.AddModelError("", "Unable to save changes after multiple attempts. Try again, and if the problem persists, see your system administrator.");
            }
            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.");
                }
            }
            PopulateAssignedDietaryRestrictionData(camper);
            PopulateDropDownLists(camper);

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