所以我想要做的是顯示存儲在數據庫中的5個最近記錄以顯示在「創建」視圖上。該表存儲在「_Recent」中,該表是該表的部分視圖。該表出現在「創建」視圖中,但我怎樣才能讓它只顯示最近5條記錄而不是所有內容。如何僅顯示數據庫中的5個最新記錄 - ASP.NET MVC
_Recent(部分圖)
<div class="col-md-6">
<div class="table-title">
<table class="table-fill">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.DisplayName)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Amount)
</th>
<th>
@Html.DisplayNameFor(model => model.TaxBonus)
</th>
<th>
@Html.DisplayNameFor(model => model.Comment)
</th>
</tr>
</thead>
</div>
@foreach (var item in Model)
{
<tbody class="table-hover">
<tr>
<td clas="text-left">
@Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Date)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Amount)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.TaxBonus)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Comment)
</td>
</tr>
</tbody>
}
</table>
創建視圖
<div class="table-title">
@Html.Partial("_Recent", Model);
</div>
創建控制器
public ActionResult Create()
{
return View(db.Donations.ToList());
}
慈善類
public class Charity
{
public int ID { get; set; }
public string DisplayName { get; set; }
public DateTime Date { get; set; }
public Double Amount { get; set; }
public Double TaxBonus { get; set; }
public String Comment { get; set; }
}
public class CharityDBContext : DbContext //controls information in database
{
public DbSet<Charity> Donations { get; set; } //creates a donation database
public Charity Highest { set; get; }
public CharityDBContext()
{
this.Highest = new Charity();
}
}
謝謝你!幫助很多:) – Edafy