【问题标题】:ASP.NET MVC - Order By DateASP.NET MVC - 按日期排序
【发布时间】:2017-07-04 06:07:22
【问题描述】:

我正在尝试按日期 (SermonDate) 对表格(列:ID、名称、SermonDate、SermonTitle、BibleReading、VideoLink、BulletinLink)进行排序,以便具有最新日期的数据显示在我的 MVC 应用程序中。

经过一些研究,我尝试参考thisthis 将以下内容放在下面,但对我没有任何帮助 - 可能是因为我误解并输入了错误的代码。

public async Task<IActionResult> Index()
{
     return View(await _context.Sermon.OrderBy(sermon => 
     DateTime.Now).Take(5).ToListAsync());
}

我当前的控制器(SermonsController.cs):

public SermonsController(BKPCContext context)
    {
        _context = context;    
    }

    public async Task<IActionResult> Index()
    {
        return View(await _context.Sermon.OrderBy(sermon => DateTime.Now).Take(5).ToListAsync());
    }

    public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var sermon = await _context.Sermon
            .SingleOrDefaultAsync(m => m.ID == id);
        if (sermon == null)
        {
            return NotFound();
        }

        return View(sermon);
    }

    public IActionResult Create()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("ID,Name,SermonDate,SermonTitle,BibleReading,VideoLink,BulletinLink")] Sermon sermon)
    {
        if (ModelState.IsValid)
        {
            _context.Add(sermon);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        return View(sermon);
    }

和/Sermons/Index.cshtml中的html表如下:

<table class="table">
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SermonDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SermonTitle)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BibleReading)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.VideoLink)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BulletinLink)
        </th>
        <th>

        </th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SermonDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SermonTitle)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BibleReading)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VideoLink)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BulletinLink)
        </td>
        <td id="aspButton">
            <a role="button" id="btn-primary" class="btn btn-primary" asp-action="Edit" asp-route-id="@item.ID">Edit</a>
            <a role="button" id="btn-danger" class="btn btn-danger" asp-action="Delete" asp-route-id="@item.ID">Delete</a>
        </td>
    </tr>
    }
</tbody>

任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net model-view-controller html-table sql-order-by


    【解决方案1】:
    public class Sermon{
         public int ID {get;set;}
         public DateTime DateAdded {get; set}
    } 
    
    mySermonList.OrderByDescending(x => x.DateAdded);
    

    【讨论】:

    • 我已经有一个讲道课,所以我只在我的控制器中添加了mySermonList.OrderByDescending(x =&gt; x.DateAdded);,如下所示:return View(await _context.Sermon.OrderByDescending(x =&gt; x.SermonDate).ToListAsync());,它就像魅力一样。谢谢!
    • 不客气 :)
    【解决方案2】:

    我认为这个问题 讲道 => DateTime.Now

    您是按 DateTime.Now 排序的吗? 如果布道有日期添加字段 讲道 => 例如添加日期

    【讨论】:

    • 我目前只有 3 行数据(3 个不同的日期),但表格按降序显示它们(最新日期在底部,最旧日期在顶部)。我想反转它并希望最新的日期出现在顶部。
    • 你的布道对象是什么样子的?我认为 Hope Mystery 是对的,您尝试按 DateTime.Now 排序,而不是 Sermon Object 的实际日期属性(?)
    猜你喜欢
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2015-03-10
    • 2015-11-20
    • 1970-01-01
    • 2017-07-05
    • 2016-07-26
    相关资源
    最近更新 更多