【问题标题】:How to only display 5 most recent records in database - ASP.NET MVC如何只显示数据库中最近的 5 条记录 - ASP.NET MVC
【发布时间】:2016-03-01 16:44:35
【问题描述】:

所以我要做的是显示存储在数据库中的 5 个最新记录以显示在“创建”视图中。该表存储在“_Recent”中,它是该表的部分视图。该表出现在“创建”视图中,但我怎样才能让它只显示最近的 5 条记录而不是所有内容。

_最近(部分视图)

<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();


    }

}

【问题讨论】:

    标签: c# asp.net asp.net-mvc view


    【解决方案1】:
    public ActionResult Create()
    {
        return View(db.Donations.OrderByDescending(x => x.Id).Take(5).ToList());
    }
    

    您也可以使用OrderByDescending(x =&gt; x.Date)。良好的做法是记录创建更新记录的日期(列和属性),以便您可以按这些字段排序。

    【讨论】:

      【解决方案2】:

      正确的做法是:

      1. 按所需字段对记录进行排序 (OrderByDescending)
      2. 将结果限制为所需的记录(Take)
      3. 最后,从数据库 (ToList) 中具体化您的结果。

      return View(db.Donations.OrderByDescending(d =&gt; d.ID).Take(5).ToList());

      这使得查询在底层数据库引擎中执行,并且只从数据库表中获取确切数量的记录。

      【讨论】:

        【解决方案3】:
        public ActionResult Create()
        {
            return View(db.Donations.OrderByDescending(o => o.ObjectID).Take(5).ToList());
        }
        

        请检查大写字母,我现在不在视觉工作室。

        更新

        【讨论】:

        • 这将取前 5 个然后反转它们,不保证它们会被 Id 反转。
        • 对不起,我弄错了,.OrderByDescending() 在 .Take(5) 之前
        • 没有不带参数的OrderByDescending 重载。您始终必须提供 Func 或 lambda。
        猜你喜欢
        • 1970-01-01
        • 2020-03-30
        • 1970-01-01
        • 2014-10-06
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多