Set Model.Id
to something greater than 0 in the handler before returning the view.
Nancy show model data in view
Question
Hi I'm trying to show my data from a model to a view. now I'm hard coding this data for now but this is what I've got.
Model:
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; }
}
So in my module, I create a new instance of my table model, then add the data I want to show, then return the model to the view.
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];
};
}
}
And on the view I have @inherits
Nancy.ViewEngines.Razor.NancyRazorViewBase<Data.Models.Table>
. I want to display the information in a table so I display the data using <td>@Model.position</td>
. But nothing is showing can't figure out why. do I need to convert my model to a list?
@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>
Any help would be much appreciated.
Thanks.
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow