如何计算重复的答案

时间:2018-05-17 15:09:29

标签: c# asp.net asp.net-mvc

我正在进行在线调查,现在我需要计算有多少重复响应,我想要显示这样的内容:“答案#1有X票”等等。 现在我的视图是默认生成的。 我的控制器:

 public async Task<ActionResult> Results(Vote vote, Answer answer)
    {
        using (var poolDbContext = new PoolContext())
        {

            var Answer = poolDbContext.Answers.AsNoTracking().FirstOrDefault(a => a.Id == a.Id);
            var Question = await poolDbContext.Questions.Include(A => A.Answers).AsNoTracking().SingleOrDefaultAsync(r => r.Id == r.Id);
            var Vote = poolDbContext.Votes.AsNoTracking().FirstOrDefault(a => a.Id == a.Id);
            answer.Votes = poolDbContext.Votes.ToList();

            answer.Text =  Answer.Text;
            answer.QuestionId = Question.Id;
            answer.Id = Answer.Id;
            vote.AnswerId = Answer.Id;
            vote.Id = Vote.Id;
            vote.IpAdress = Vote.IpAdress;
            vote.VoteDate = Vote.VoteDate;

            return View(vote);
        }
    }

我的模特:

public class Answer
{
    public int Id { get; set; }
    public string Text { get; set; }
    public int QuestionId { get; set; }
    public List<Vote> Votes { get; set; }
    public Answer()
    {
        Votes = new List<Vote>();
    }
}
public class Question
{
    public Question()
    {
        Answers = new List<Answer>();
    }
    public int Id { get; set; }
    public string Text { get; set; }
    public virtual List<Answer> Answers { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public Boolean Active { get; set; }
    public string SelectedAnswer { set; get; }
}
 public class Vote
{
    public int Id { get; set; }
    public string IpAdress { get; set; }
    public DateTime VoteDate { get; set; }
    public int AnswerId { get; set; }

}

1 个答案:

答案 0 :(得分:0)

<div>
    <h4>Vote</h4>
    <hr />
    <dl class="dl-horizontal">
        @Model.Text

        @foreach (var answer in Model.Answers)
        {
            <br />
            <label>
                @answer.Text:   @answer.Votes.Count();
            </label>
        }
    </dl>
</div>