Ejemplo n.º 1
0
        public ActionResult InsertResponses(PollsViewModel p)
        {
            Models.Poll       pq = new Models.Poll();
            Models.PollOption po = new Models.PollOption();

            ViewBag.Polllist1 = (from pol in db.Polls
                                 orderby pol.C_id descending
                                 select new question {
                pollsubject = pol.pollsubject, PollCount = pol.PollCount ?? 0
            }).Take(1).ToList();

            for (int i = 0; i < @ViewBag.Polllist1[0].PollCount; i++)
            {
                try
                {
                    po.optiontext = p.optiontext[i];
                    po.pollid     = Convert.ToInt32(TempData["pollidtemp"]);
                    db.PollOptions.Add(po);
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    ViewBag.msg = "لم يتم حفظ الجواب !!";
                }
            }
            return(RedirectToAction("Responds"));
        }
Ejemplo n.º 2
0
        public ActionResult AddPollandOptions(PollsViewModel p)
        {
            Models.Poll pq = new Models.Poll();

            try
            {
                pq.pollsubject = p.pollsubject;
                pq.PollCount   = p.PollCount;
                //pq.pollsubject = p.Poll.pollsubject;
                //pq.PollCount = p.Poll.PollCount;
                db.Polls.Add(pq);
                db.SaveChanges();
            }
            catch (Exception)
            {
                // throw;
                ViewBag.msg = "لم يتم حفظ السؤال !!";
            }


            return(RedirectToAction("InsertResponses"));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Post([FromBody] PollsViewModel poll)          //DECS assuming that we will nest array of options inside a poll. presumably doable with seperate jsons if not working.
        {                                                                               //OR with seperate api calls (e.g. api/polls and then api/options/{pollid} for each opt.)   //TODO
            if (ModelState.IsValid)
            {
                int optionCount = 0;
                var newPoll     = Mapper.Map <Poll>(poll);
                newPoll.UserName     = User.Identity.Name;
                newPoll.DateCreated  = DateTime.Now;
                newPoll.NumOfOptions = newPoll.Options.Count();
                newPoll.SumVotes     = 0;
                foreach (Option opt in newPoll.Options)
                {
                    opt.Order = optionCount++;
                }
                _repository.AddPollWithOptions(newPoll);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/polls/{newPoll.DateCreated.ToString()}", Mapper.Map <PollsViewModel>(newPoll)));
                }
            }
            return(BadRequest("Failed to save the poll"));
        }