Пример #1
0
        public ActionResult SelectOption(Choice choice)
        {
            //get the only default YearTerm row by BCIT option policy
            choice.YearTerm = db.YearTerms.SingleOrDefault(y => y.IsDefault == true).YearTermId;

            //check the input model is valid and duplicate rows with the same StudentId in the StudentOptionChoices is not allowed in the same YearTerm
            if (ModelState.IsValid && !db.Choices.Any(c => c.StudentId == choice.StudentId && c.YearTerm == choice.YearTerm))
            {
                choice.SelectionDate = DateTime.Now;
                db.Choices.Add(choice);
                db.SaveChanges();
                TempData["success"] = true;
                return RedirectToAction("Index");
            }
            ViewBag.OptionList = new SelectList(db.Options.Where(o => o.IsActive == true).OrderBy(o => o.Title), "OptionId", "Title");
            ModelState.AddModelError("", "You may only make option selections once.");
            if (!User.IsInRole("Admin"))
            {
                ViewBag.CurrentStudentId = User.Identity.Name;
            }
            return View();
        }
        public IHttpActionResult PutChoice(int id, Choice choice)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != choice.ChoiceId)
            {
                return BadRequest();
            }

            db.Entry(choice).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ChoiceExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult PostChoice(Choice choice)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Choices.Add(choice);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = choice.ChoiceId }, choice);
        }
        public ActionResult Create()
        {

            if (User.IsInRole("Student") && !User.IsInRole("Admin"))
            {
                var exists = db.Choices.Where(c => c.StudentId == User.Identity.Name).FirstOrDefault();
                if (exists != null)
                {
                    //choice already exists
                    return RedirectToAction("Details", "Choice", new { id = exists.ChoiceId });
                }
            }
            

            ViewBag.FirstChoiceOptionId = new SelectList(onlyActiveOptions(), "OptionId", "Title");
            ViewBag.SecondChoiceOptionId = new SelectList(onlyActiveOptions(), "OptionId", "Title");
            ViewBag.ThirdChoiceOptionId = new SelectList(onlyActiveOptions(), "OptionId", "Title");
            ViewBag.FourthChoiceOptionId = new SelectList(onlyActiveOptions(), "OptionId", "Title");

            Choice choice = new Choice();
            choice.StudentId = User.Identity.Name;

            return View(choice);
        }