我的应用程序是关于一个在线池,所以我已经设法添加问题/编辑问题/删除问题,同样的答案.. 我也做了一个投票系统,所以现在我想显示投票的答案,比如每个答案都有的票数。(“我认为”.count()“)。但我不能这样做...... 这是我的控制器
public async Task<ActionResult> Results(Vote vote,Answer answer)
{
using (var poolDbContext = new PoolContext())
{
if (poolDbContext.Votes.Count() == 0)
{
Vote Q = new Vote();
Q.Questions = poolDbContext.Questions.ToList();
poolDbContext.Votes.Add(Q);
var count = poolDbContext.SaveChanges();
}
var Vote = poolDbContext.Votes.Include(A=>A.Questions).Single(A => A.AnswerId == answer.Id);
var Answer = poolDbContext.Questions.Include(q => q.Answers).Single(q => q.Id == q.Id);
var Question = await poolDbContext.Questions.Include(A => A.Answers).AsNoTracking().SingleOrDefaultAsync(r => r.Id == r.Id);
var CountVotes = poolDbContext.Votes.Count(q => q.Id == q.Id);
var AllQuestions = Question.Text.ToList();
var ShowResults = vote.AnswerId;
answer.Text = Answer.Answers.ToString();
answer.QuestionId=Answer.Id;
return View(Answer);
}
我对该控制器的看法:
@model PoolManager.Models.Answer
@{
ViewData["Title"] = "Results";
}
<h2>Results</h2>
<div>
<h4>Vote</h4>
<hr />
<dl class="dl-horizontal">
@foreach (var q in Model.Votes)
{
@Model.Text
<br />
@Model.Votes.Count
}
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
<a asp-action="Index">Back to List</a>
这里也是我的模特:
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; }
public List<Question> Questions { set; get; }
public Vote()
{
Questions = new List<Question>();
}
}
答案 0 :(得分:0)
@model PoolManager.Models.Question
{
ViewData["Title"] = "Results";
}
<h2>Results</h2>
<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>