public async Task <ActionResult> ChoiceDetail(DisplayChoiceInformation model) { var choice = await choiceDac.GetChoice(model.Id); if (choice.Votes == null) { choice.Votes = new List <Vote>(); } var voted = choice.Votes.FirstOrDefault(it => it.Owner == User.Identity.Name); var votes = choice.Votes.ToList(); if (voted != null) { var voteIndex = votes.IndexOf(voted); votes[voteIndex].Rating = model.Rate; votes[voteIndex].CreateAt = DateTime.UtcNow; } else { votes.Add(new Vote { Owner = User.Identity.Name, Rating = model.Rate, CreateAt = DateTime.UtcNow }); } choice.Votes = votes; await choiceDac.UpdateChoice(choice); return(RedirectToAction(nameof(PollDetail), new { pollid = model.PollRefId })); }
public async Task <ActionResult> ChoiceDetail(string choiceid) { var choice = await choiceDac.GetChoice(choiceid); var poll = await pollDac.GetPoll(choice.PollId); var rate = choice.Votes != null && choice.Votes.Any() ? (int)Math.Round(choice.Votes.Sum(it => it.Rating) / choice.Votes.Count()) : 0; var model = new DisplayChoiceInformation { Id = choice.Id, Name = choice.Title, PollRefId = poll.Id, PollCreateDate = poll.CreateAt, PollName = poll.Topic, PollOwnerName = poll.Owner, Rate = rate }; return(View(model)); }