Asp.net mvc 4 ASP.NETMVC中的投票系统

Asp.net mvc 4 ASP.NETMVC中的投票系统,asp.net-mvc-4,Asp.net Mvc 4,我想在我的页面的部分中显示轮询,我已经创建了这些POCO类来实现这一点: public class Polls { public int Id { get; set; } public string Question { get; set; } public bool Active { get; set; } public IList<PollOptions> PollOptions { get; set; } } public class Poll

我想在我的页面的部分中显示轮询,我已经创建了这些POCO类来实现这一点:

public class Polls
{
    public int Id { get; set; }
    public string Question { get; set; }
    public bool Active { get; set; }
    public IList<PollOptions> PollOptions { get; set; }
}

public class PollOptions
{
    public int Id { get; set; }
    public virtual Polls Polls { get; set; }
    public string Answer { get; set; }
    public int Votes { get; set; }
}
然后,我使用上面的ViewModel将我的模型传递给我的视图:

public ActionResult Index()
{
    var poll = from p in db.Polls
               join po in db.PollOptions on p.Id equals po.Polls.Id
               where p.Active == true
               select new PollViewModel { 
                   Id=p.Id,
                   Question=p.Question,
                   Answer=po.Answer
    };

    return View(model);
}
在我看来,我想显示我的民意测验的
问题
答案
,我尝试了以下代码:

@section Polling{
    @foreach (var item in Model.Polls)
    {
        <input type="radio" /> @item.Answer
    }
}
我该怎么做


PS:我的民意调查表中有一行显示在主页上

民意调查和民意调查选项之间存在关系。所以从你的数据库中获取民意测验。并将其传递给视图。此外,您已经有了连接到其投票的投票。不需要连接两个表

控制器

public ActionResult Index()
{
    // get active Polls
    var poll = from p in db.Poll
               where p.Active == true
               select p;

    // pass it to the view
    return View(poll);
}
看法

@model IEnumerable
@区段轮询{
@foreach(模型中的var问题)
{
@问题,问题
@foreach(var问题答案。PollOptions)
{
@回答,回答
}
}
}

tnx,但我在这一行foreach(var answer in question.PollOptions)中收到了Null异常。您的类之间没有关系吗?有没有没有没有PollOptions的轮询?@Sirwan Afifi,尝试快速加载相关的轮询选项:var pollsList=db.poll.Include(x=>x.PollOptions)。其中(x=>x.IsActive.ToList();现在pollList也包括了PollOptions。嗨,Sirwan Afifi先生,你可以通过图形视图Poll来帮助我
@section Polling{
    **@Model.Polls.Question**
    @foreach (var item in Model.Polls)
    {
        <input type="radio" /> @item.Answer
    }
}
public ActionResult Index()
{
    // get active Polls
    var poll = from p in db.Poll
               where p.Active == true
               select p;

    // pass it to the view
    return View(poll);
}
@model IEnumerable<Polls>

@section Polling{
    @foreach (var question in Model)
    {
        <h2>@question.Question</h2>
        @foreach(var answer in question.PollOptions)
        {
            <input type="radio" /> @answer.Answer
        }
    }
}