关系和 MVC3

发布于 2024-11-07 09:43:35 字数 1088 浏览 0 评论 0原文

好的,我有一个包含 TeamID、TeamName 的团队表和一个包含 gameid、team1、team2 列的游戏表。

现在我没有 team1 和 team2 作为 team 表的外键。我知道这会让事情变得更容易,但我想在不这样做的情况下学习。因此,team1team2 是 int 字段。没有约束检查。

因此,当我在视图中显示它时,它会显示 team1 和 team2 列,但我希望它从 team 表中提取团队名称,而不是显示整数 ID。

好吧,在我看来,我有以下内容:

   @model IEnumerable<Betting.Models.Bets>
@{
    ViewBag.Title = "List of Games";
}
@{
    var grid = new WebGrid(source: Model, defaultSort: "EndDate", rowsPerPage: 3);    
}
<h2>
    Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<h2>
    Betting List</h2>
<div id="grid">
    @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
            grid.Column("Subject"),
            grid.Column("Team1"),
            grid.Column("Team2")          
        )
    )
</div>

我的控制器非常简单:

public ViewResult Index()
{

    return View(db.Bets.ToList());
}

okay so I have a teams table with TeamID, TeamName and a games table with columns gameid, team1, team2.

Now I dont have team1 and team2 as Foreign keys to the teams table. I understand this will make it easier but I want to learn without doing that. So team1, and team2 are int fields. There is no constraint check.

So when I display it in my view, it displays the team1 and team2 columns but instead of displaying the integer ID, I want it to pull the team name from the teams table.

okay in my view I have the following:

   @model IEnumerable<Betting.Models.Bets>
@{
    ViewBag.Title = "List of Games";
}
@{
    var grid = new WebGrid(source: Model, defaultSort: "EndDate", rowsPerPage: 3);    
}
<h2>
    Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<h2>
    Betting List</h2>
<div id="grid">
    @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
            grid.Column("Subject"),
            grid.Column("Team1"),
            grid.Column("Team2")          
        )
    )
</div>

And my controller is really simple:

public ViewResult Index()
{

    return View(db.Bets.ToList());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

猫瑾少女 2024-11-14 09:43:35

始终在 ASP.NET MVC 中使用视图模型,您不会遇到此类问题:

public class TeamViewModel
{
    public string Team1Name { get; set; }
    public string Team2Name { get; set; }
    public string Subject { get; set; }
}

然后在控制器操作中执行必要的映射/查询以填充此视图模型:

public ActionResult Index()
{
    // Fetch the data here, depending on the ORM you are using
    // perform the necessary joins so that you have the team names
    IEnumerable<Betting.Models.Bets> model = ...

    // map the model to the view model previously defined
    IEnumerable<TeamViewModel> viewModel = ...

    // pass the view model to the view for display
    return View(viewModel);
}

最后在视图中:

@model IEnumerable<TeamViewModel>
@{
    ViewBag.Title = "List of Games";
}
@{
    var grid = new WebGrid(source: Model, defaultSort: "EndDate", rowsPerPage: 3);    
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<h2>Betting List</h2>
<div id="grid">
    @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
            grid.Column("Subject"),
            grid.Column("Team1Name"),
            grid.Column("Team2Name")          
        )
    )
</div>

就模型之间的映射而言视图模型 AutoMapper 可以大大简化这项任务。

Always use view models in ASP.NET MVC and you won't have such problems:

public class TeamViewModel
{
    public string Team1Name { get; set; }
    public string Team2Name { get; set; }
    public string Subject { get; set; }
}

then in the controller action do the necessary mapping/querying in order to populate this view model:

public ActionResult Index()
{
    // Fetch the data here, depending on the ORM you are using
    // perform the necessary joins so that you have the team names
    IEnumerable<Betting.Models.Bets> model = ...

    // map the model to the view model previously defined
    IEnumerable<TeamViewModel> viewModel = ...

    // pass the view model to the view for display
    return View(viewModel);
}

finally in the view:

@model IEnumerable<TeamViewModel>
@{
    ViewBag.Title = "List of Games";
}
@{
    var grid = new WebGrid(source: Model, defaultSort: "EndDate", rowsPerPage: 3);    
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<h2>Betting List</h2>
<div id="grid">
    @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
            grid.Column("Subject"),
            grid.Column("Team1Name"),
            grid.Column("Team2Name")          
        )
    )
</div>

As far as the mapping between your models and view models is concerned AutoMapper could greatly simplify this task.

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