2014-03-18 145 views
0

嗨我試圖顯示我的數據從模型到視圖。現在我現在很難編碼這些數據,但這是我的。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]; 
      }; 
    } 
} 

並在視圖上我有@inheritsNancy.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> 

任何幫助將不勝感激。

謝謝。

+0

你能分享所有的視圖代碼嗎? –

+0

我在視圖中添加了斷點,但它們不會被打到? – Paddy1990

+0

Model.Id的價值是什麼? –

回答

1

在返回視圖之前,在處理程序中將Model.Id設置爲大於0的值。

相關問題