所以我想要做的是显示存储在数据库中的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