Esempio n. 1
0
        // GET: Candidate
        //[Route("candidate/for/{positionName?}")]
        public ActionResult Index()
        {
            //if (!string.IsNullOrEmpty(positionName))
            //{
            //    // get position Id
            //    int positionId = db.Position.Where(x => x.Name == positionName).Select(x => x.Id).FirstOrDefault();
            //    IEnumerable<CandidateVM> candidates = User.GetCandidatesFor(positionId);

            //    ViewBag.Title = "Candidates for " + positionName;
            //    ViewBag.PositionId = positionId;

            //    return View("CandidatesForPosition", candidates);
            //}

            // get all positions
            var model = User.GetAllPositions();

            return(View("Index", model));
        }
Esempio n. 2
0
        public ActionResult VoteResults()
        {
            // init count
            int count = 0;

            // init model
            VoteResult model = new VoteResult();
            //model.Votes = new List<SingleVote>();

            // init candidateScore
            List <CandidateScore> candidateScores = new List <CandidateScore>();

            // get votes
            var votes = db.Votes.ToArray();

            // get candidates
            var allCandidates = User.GetAllCandidates();

            // init candidate scores
            model.CandidateScores = allCandidates.Select(x => new CandidateScore
            {
                Id         = x.VoterId,
                Name       = x.FirstName + " " + x.LastName,
                VoteCount  = 0,
                PositionId = x.PositionId
            }).ToList();

            // init all position ids
            model.Positions = User.GetAllPositions();

            // loop through votes and get each voter details
            foreach (var item in votes)
            {
                // init count
                count++;

                // get user
                var user = db.VoteUsers.Where(x => x.VoterId == item.VoterId).FirstOrDefault();

                if (model.CandidateScores.Any(x => x.Id == item.CandidateId))
                {
                    var cand = model.CandidateScores.Where(x => x.Id == item.CandidateId).FirstOrDefault();
                    cand.VoteCount++;
                }

                // init singlevm
                SingleVote vote = new SingleVote
                {
                    SN         = count,
                    VoterName  = user.FirstName + " " + user.LastName,
                    VoterPhone = user.Phone,
                    VotedFor   = db.VoteUsers.Where(x => x.VoterId == item.CandidateId).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault()
                };

                //model.Votes.Add(vote);
            }
            ;

            model.Name = User.GetName();

            return(View("VoteResults", model));
        }