ASP.NET MVC - 按日期排序
ASP.NET MVC - Order By Date
我正在尝试按日期 (SermonDate) 对 table(列:ID、名称、SermonDate、SermonTitle、BibleReading、VideoLink、BulletinLink)进行排序,以便最新日期的数据显示在最前面在我的 MVC 应用程序中。
经过一些研究,我尝试参考 this and this 将下面的代码放入其中,但对我没有任何用处 - 可能是因为我误解并输入了错误的代码。
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中的htmltable如下:
<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>
如有任何帮助,我们将不胜感激。
我觉得这个问题
布道 => DateTime.Now
您按 DateTime.Now 排序?
如果布道有日期添加字段
布道 => 添加日期,例如
public class Sermon{
public int ID {get;set;}
public DateTime DateAdded {get; set}
}
mySermonList.OrderByDescending(x => x.DateAdded);
我正在尝试按日期 (SermonDate) 对 table(列:ID、名称、SermonDate、SermonTitle、BibleReading、VideoLink、BulletinLink)进行排序,以便最新日期的数据显示在最前面在我的 MVC 应用程序中。
经过一些研究,我尝试参考 this and this 将下面的代码放入其中,但对我没有任何用处 - 可能是因为我误解并输入了错误的代码。
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中的htmltable如下:
<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>
如有任何帮助,我们将不胜感激。
我觉得这个问题 布道 => DateTime.Now
您按 DateTime.Now 排序? 如果布道有日期添加字段 布道 => 添加日期,例如
public class Sermon{
public int ID {get;set;}
public DateTime DateAdded {get; set}
}
mySermonList.OrderByDescending(x => x.DateAdded);