我的项目的一部分使用民意调查,我正在使用EF for Classes。 我有模特:
Polls.cs
public class Poll
{
[Key]
public int ID { get; set; }
public string Title { get; set; }
[Column("creation_date")]
public DateTime CreationDate { get; set; }
[Column("expiration_date")]
public DateTime ExpirationDate { get; set; }
public virtual ICollection<PollOption> Options { get; set; }
public virtual ICollection<PollVote> Votes { get; set; }
}
PollOption.cs
[Table("PollsOptions")]
public class PollOption {
[Key]
public int ID { get; set; }
[Display(Name = "Poll")]
[Required(ErrorMessage = "Poll must be set.")]
[Column("poll_id")]
public int PollID { get; set; }
[Required(ErrorMessage = "Option's Text cannot be empty.")]
public string Text { get; set; }
[Editable(false)]
public int Votes { get; set; }
public virtual Poll Poll { get; set; }
}
PollVote.cs
[Table("PollsVotes")]
public class PollVote
{
[Key]
[Display(Name = "Player", Description = "Player that voted.")]
[Column("player_id", Order = 0)]
public int PlayerID { get; set; }
[Key]
[Display(Name = "Option", Description = "Option voted by the Player.")]
[Column("option_id", Order = 1)]
public int OptionID { get; set; }
public virtual Player Player { get; set; }
public virtual PollOption Option { get; set; }
}
我想在这样的视图上使用它: - 从包含轮询列表的控制器传递模型,如下所示:
Poll[0]:
----PollTitle
----Options:
---- ----Option1 From Poll0
---- ----Option2 From Poll0
---- ----Option3 From Poll0
----Votes:
---- ----Vote1 for Option1 From User X
---- ----Vote2 for Option2 From User Y
---- ----Vote3 for Option3 From User Z
Poll[1]:
----PollTitle
----Options:
---- ----Option4 From Poll1
---- ----Option5 From Poll1
---- ----Option6 From Poll1
----Votes:
---- ----Vote1 for Option4 From User X
---- ----Vote2 for Option5 From User Y
---- ----Vote3 for Option6 From User Z
我该怎么做!?
我希望你明白...... 谢谢!
答案 0 :(得分:0)
这是一个粗略的准备方法......根据需要进行定制
查看:
@model IEnumerable<MvcApplication1.Models.Poll>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@if (Model == null)
{
@:No Polls to display.
}
else
{
foreach (var poll in Model)
{
@Html.DisplayFor(m => poll)
}
}
显示模板(views / shared / DisplayTemplates / Poll.cshtml)
(如果你没有碰到这些,那么它们就非常有用了。)
@model MvcApplication1.Models.Poll
@Html.DisplayFor(model => model.Title)
Options:
@foreach (var option in Model.Options)
{
@Html.DisplayFor(m=> option.Text)
}
Votes:
@foreach (var vote in Model.Votes)
{
@Html.DisplayFor(m=> vote.Option)
}
希望这有助于让你走上正确的道路。