ASP NET MVC 3 模型级联类
我的项目的一部分使用民意调查,并且我将 EF 用于类。 我有模型:
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
我该怎么做!?
我希望你能理解... 谢谢!
Part of my project uses Polls, and I'm using EF for Classes.
I have the Models:
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; }
}
I want to use it on a view like this:
- Pass a model from a controller that contains a Poll List like this:
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
How do I do it!?
I hope you understand...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个粗略的现成方法...根据需要自定义
视图:
显示模板(views/shared/DisplayTemplates/Poll.cshtml)
(如果您还没有遇到过这些,它们非常有用。)
希望这有助于您继续正确的道路。
Here's a rough an ready approach... customise as needed
View:
Display Template (views/shared/DisplayTemplates/Poll.cshtml)
(If you've not come accross these, they're mega useful.)
Hope this helps get yo on the right path.