【发布时间】:2017-07-04 06:07:22
【问题描述】:
我正在尝试按日期 (SermonDate) 对表格(列:ID、名称、SermonDate、SermonTitle、BibleReading、VideoLink、BulletinLink)进行排序,以便具有最新日期的数据显示在我的 MVC 应用程序中。
经过一些研究,我尝试参考this 和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中的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