public async Task <ActionResult> CandidatesFor(string 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; TempData["PositionId"] = positionId; TempData["PositionName"] = positionName; return(View("CandidatesForPosition", candidates)); }
public PartialViewResult VoteFor(int positionId, string positionName) { // check if vote is finished if (positionId == 0 && positionName.ToLower() == "finished") { // get all votes for user var userVotes = db.Votes.Where(x => x.VoterId == User.VoterId).ToList(); // init voteResult List <VoteResultVM> voteResults = new List <VoteResultVM>(); foreach (var vote in userVotes) { var result = new VoteResultVM { PositionName = db.Position.Where(x => x.Id == vote.PositionId).Select(x => x.Name).FirstOrDefault() }; if (vote.CandidateId == null) { result.CandidateName = "NO VOTE"; } else { result.CandidateName = db.VoteUsers.Where(x => x.VoterId == vote.CandidateId).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault(); } //add result voteResults.Add(result); } // construct result mail message string resultMail = ""; foreach (var result in voteResults) { string votedFor = "<tr>" + "<td style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\">" + result.PositionName + "</td>" + "<td style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\">" + result.CandidateName + "</td>" + "</tr>"; resultMail += votedFor; } // get username and date string userName = db.VoteUsers.Where(x => x.Email == User.Email).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault(); DateTime?voteDate = db.VoteUsers.Where(x => x.Email == User.Email).Select(x => x.LastVoteDateUtc).FirstOrDefault(); string date; if (voteDate != null) { DateTime updatedVoteDate = voteDate ?? DateTime.Now; var day = updatedVoteDate.Day; var dayOfWeek = updatedVoteDate.DayOfWeek; var month = updatedVoteDate.ToString("MMMM"); var year = updatedVoteDate.Year; date = day + " " + month + ", " + year; } else { date = "-"; } string message = "<html><body>" + "<div style=\"text-align:center;max-width:600px;\">" + "<h3 style=\"background-color: #64608e;margin-bottom: 0px;padding: 10px 0px;color: #fff;\">" + "My Voting Summary - LAGSOBA '94 " + "</h3>" + "<div style=\"border:2px solid #64608e\">" + "<table style=\"width: 100%;margin-bottom: 1rem;\">" + "<tr>" + "<th style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\"> Position</th>" + "<th style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\"> Candidate Voted For</th>" + "</tr>" + resultMail + "</table>" + "<div style=\"margin-bottom: 20px;\">" + "<h6>The Vote Summary Above was casted by:</h6>" + "<p style=\"margin: 0px;\"><strong>Name: </strong> " + userName + "</p>" + "<p><strong>Last Vote On: </strong> " + date + "</p>" + "</div>" + "<p style=\"color: #846d6dc7;\"><em>Thanks For Voting</em></p>" + "</div>" + "</div>" + "</body></html>"; ViewBag.MyVoteResult = message; return(PartialView("_MyVoteResult")); } // get user Id string voterId = db.VoteUsers.Where(x => x.Email == User.Email).Select(x => x.VoterId).FirstOrDefault(); VotingVM model = new VotingVM(); // get position var postion = db.Position.Where(x => x.Id == positionId).FirstOrDefault(); // get candidates IEnumerable <CandidateVM> candidates = User.GetCandidatesFor(postion.Id); // init model.candidates model.Candidates = candidates.Select(x => new Candidates { Id = x.VoterId, Name = x.FirstName + " " + x.LastName, ImageUrl = x.ImageString, PositionId = x.PositionId }).ToList(); // get postion name model.PostionName = db.Position.Where(x => x.Id == postion.Id).Select(x => x.Name).FirstOrDefault(); // check if user has casted votes for this position model.VoteCasted = false; var voteCasted = db.Votes.Where(x => x.VoterId == voterId && x.PositionId == postion.Id).FirstOrDefault(); if (voteCasted != null) { model.VoteCasted = true; model.VotedCandidateId = voteCasted.CandidateId; } // admin privileges model.IsAdmin = User.HasRole("Admin"); // set user name ViewBag.VoterName = db.VoteUsers.Where(x => x.VoterId == voterId).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault(); return(PartialView("_VoteForPosition", model)); }