日付(SermonDate)でテーブル(列、ID、名前、SermonDate、SermonTitle、BibleReading、VideoLink、BulletinLink)をソートしようとしています。私のMVCアプリケーションでそれを表示します。ASP.NET 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>
すべてのヘルプ本当に感謝します。
私は既にSermonクラスを持っていましたので、 'mySermonList.OrderByDescending(x => x .DateAdded); 'このような私のコントローラーに:' return View(_context.Sermon.OrderByDescending(x => x.SermonDate).ToListAsync());ありがとう! – terryeah
あなたはようこそです:) –