私は、ホームページ(索引)に最近のコンテキストを表示するための部分的なビューを生成しようとしています。ただし、モデルはNULLとして返されます。他のコントローラからの部分表示の表示
ポッドキャストコントローラ方法:
// Generates list of most recent 2 podcasts
public async Task<IActionResult> _RecentPodcasts()
{
var recentList = from p in _context.Podcast
select p;
recentList = recentList.OrderByDescending(p => p.PublishDate).Take(2);
return View(await recentList.ToListAsync());
}
パーシャルビュー(ポッドキャスト/ _RecentPodcasts.cshtml)
@model IEnumerable<ComesNaturally.Models.Podcast>
@{
ViewData["Title"] = "_RecentPodcasts";
}
<div class="col-md-6">
<div class="table-title">
<table class="table-fill">
<thead>
<tr>
<th>Recent Podcasts</th>
</tr>
</thead>
@if (Model == null)
{
<tr> No items found</tr>}
else
{
@foreach (var item in Model)
{
<tr>
<td><a asp-action="Details" asp-route-id="@item.ID" class="alert-link">@Html.DisplayFor(modelItem => item.Title)</a></td>
</tr>
}}
</table>
</div>
</div>
メインビュー(ホーム/ Index.cshtml)
@Html.Partial("~/Views/Podcasts/_RecentPodcasts.cshtml");
あなたのメインビューのモデルは部分ビューのものと同じですか?もしあなたが 'Partial'ステートメントの部分ビューへのパラメータとしてモデルを渡そうとしていないのであれば? (または表現ですか?) – Gilles