南希在视图中显示模型数据

时间:2014-03-18 18:46:53

标签: c# view model nancy

您好我正在尝试将模型中的数据显示给视图。现在我现在很难对这些数据进行编码,但这就是我所拥有的。

型号:

public class Table
{
    public int Id { get; set; }

    public int Position { get; set; }
    public string User { get; set;}

    public int GamesPlayed { get; set; }
    public int GamesWon { get; set; }
    public int GamesDrawn { get; set; }
    public int GamesLost { get; set; }

    public int GoalsForward { get; set; }
    public int GoalsAgainst { get; set; }
    public int GoalDifference { get; set; }

    public int Points { get; set; }
}

所以在我的模块中,我创建了一个新的表模型实例,然后添加我想要显示的数据,然后将模型返回到视图。

public class LeaderboardModule : NancyModule
{
    public LeaderboardModule()
    {
        Get["/"] = _ =>
            {
                var model = new Table();

                model.Position = 1;
                model.User = "Paddy";

                model.GamesPlayed = 5;
                model.GamesWon = 3;
                model.GamesDrawn = 2;
                model.GamesLost = 0;

                model.GoalsForward = 100;
                model.GoalsAgainst = 20;
                model.GoalDifference = 80;

                model.Points = 11;

                return View["Leaderboard", model];
            };
    }
}

在视图上我有@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<Data.Models.Table>。我想在表格中显示信息,以便使用<td>@Model.position</td>显示数据。但没有任何显示无法弄清楚为什么。我需要将模型转换为列表吗?

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<Fifa.Leaderboard.Data.Models.Table>
@{
   Layout = "Views/Shared/_Layout.cshtml";
}
<table>
<tr>
<th>Position</th>
<th>User</th>
<th>Games Played</th>
<th>Won</th>
<th>Drawn</th>
<th>Lost</th>
<th>Goals Forward</th>
<th>Goals Against</th>
<th>Goal Difference</th>
<th>Points</th>
</tr>

@for (int i = 0; i < Model.Id; i++)
{
    <tr>
    <td>@Model.Position</td>
    <td>@Model.User</td>
    <td>@Model.GamesPlayed</td>
    <td>@Model.GamesWon</td>
    <td>@Model.GamesDrawn</td>
    <td>@Model.GamesLost</td>
    <td>@Model.GoalsForward</td>
    <td>@Model.GoalsAgainst</td>
    <td>@Model.GoalDifference</td>
    <td>@Model.Points</td>
    </tr>
}
</table>

非常感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:2)

在返回视图之前,在处理程序中将Model.Id设置为大于0的值。