C# 在视图中显示模型数据
嗨,我正在尝试将数据从模型显示到视图。现在我正在硬编码这些数据,但这是我得到的 型号:C# 在视图中显示模型数据,c#,view,model,nancy,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 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];
};
}
}
在视图中,我继承了Nancy.ViewEngines.Razor.NancyRazorViewBase。我想在表格中显示信息,因此我使用@Model.position
显示数据。但是什么都没有,我不知道为什么。我需要将模型转换为列表吗
@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>
@继承Nancy.ViewEngines.Razor.NancyRazorViewBase
@{
Layout=“Views/Shared/_Layout.cshtml”;
}
位置
使用者
玩的游戏
赢了
绘制
迷路的
前进目标
进球对抗
目标差
要点
@for(int i=0;i
任何帮助都将不胜感激
谢谢。在返回视图之前,在处理程序中将
Model.Id
设置为大于0的值。您可以共享所有视图代码吗?我已经在视图中添加了断点,但它们没有被命中?Model.Id的值是多少?它正在工作,我没有为Id设置值。真是个白痴!谢谢你们的帮助
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];
};
}
}