Include()
関連データをロードしようとしていInclude()
が、何か間違っていると思います。
このテーブルに靴の画像と練習用の画像を1行にロードしたいのですが、 CrateImages.ImageURL
を追加するCrateImages.ImageURL
(例を示すため)、エラーCS1061が発生し、次のデータを表示できませんInclude()
。
インデックスページがあります。
@model IEnumerable<Shoes.Data.Entities.ShoeEntity>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.ShoesImages)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<!--- This is the problem too --->
<td>
@Html.DisplayFor(modelItem => item.ShoesImages.ImageURL)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
インデックスコントローラ:
public IActionResult Index()
{
var data = _context.Shoes.Include(ci => ci.ShoeImages);
return View(data);
}
Shoe
エンティティ:
public class ShoeEntity
{
public string Id { get; set; }
public string Name { get; set; }
public ICollection<ShoeImageEntity> ShoeImages { get; set; }
}
ShoeImagesエンティティ:
public class ShoeImageEntity
{
public string Id { get; set; }
public string ImageURL { get; set; }
public string ImageFullPath =>
string.IsNullOrEmpty(ImageURL) ? null : $"https://localhost:44357{ImageURL.Substring(1)}";
public ShoeEntity Shoe { get; set; }
}
コンテキストクラス:
public class DataContext : IdentityDbContext<UserEntity, RoleEntity, string>
{
public DbSet<ShoeEntity> Shoes{ get; set; }
public DbSet<ShoeImagesEntity> ShoesImages { get; set; }
public DataContext(DbContextOptions options) : base(options)
{
}
}
また、Entity Frameworkのドキュメントも試しました。
提出されたShoeImagesはICollectionタイプであるため、複数のImageURLフィールドに対応しています。
次のビューコードを使用して、それらを表示できます。
@model IEnumerable<Shoes.Data.Entities.ShoeEntity>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.ShoesImages)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
foreach (var images in item.ShoeImages)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => images.ImageURL)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
}
</tbody>
</table>
助けてくれてありがとう。一人でやったら気づかなかっただろう。正解は正解です。両方のforeachがすべてのforeachの前に@を必要とするため、編集を行います
@model IEnumerable<Shoes.Data.Entities.ShoeEntity>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.ShoesImages)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
@foreach (var images in item.ShoeImages)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => images.ImageURL)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
}
</tbody>
</table>