ASP NET MVC 3 模型级联类

发布于 2024-12-10 19:03:14 字数 2116 浏览 0 评论 0原文

我的项目的一部分使用民意调查,并且我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-12-17 19:03:14

这是一个粗略的现成方法...根据需要自定义

视图:

@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)
}

希望这有助于您继续正确的道路。

Here's a rough an ready approach... customise as needed

View:

@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)
    }
}

Display Template (views/shared/DisplayTemplates/Poll.cshtml)

(If you've not come accross these, they're mega useful.)

@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)
}

Hope this helps get yo on the right path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文