マイページのセクションにポーリングを表示したいので、そのためにこれらの 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 class PollViewModel
{
public int Id { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
}
次に、上記の ViewModel を使用してモデルを View に渡しました。
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);
}
Question
表示したいビューとAnswer
ポーリングで、次のコードを試しました:
@section Polling{
@foreach (var item in Model.Polls)
{
<input type="radio" /> @item.Answer
}
}
上記のコードは正しく機能しますが、次のようなものも表示したいQuestion
:
@section Polling{
**@Model.Polls.Question**
@foreach (var item in Model.Polls)
{
<input type="radio" /> @item.Answer
}
}
どうやってやるの?
PS: ホームページに表示するために、ポーリング テーブルに 1 つの行があります。