J'essaie de charger des données connexes avec Include()
mais je pense que je fais quelque chose de mal.
Je veux charger les images de la chaussure sur ce tableau et les images à des fins de pratique sur une ligne, mais lorsque j'ajoute les CrateImages.ImageURL
(pour donner un exemple), j'obtiens une erreur CS1061 et je ne suis pas en mesure d'afficher les données de le Include()
.
J'ai une page d'index:
@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>
Contrôleur d'index:
public IActionResult Index()
{
var data = _context.Shoes.Include(ci => ci.ShoeImages);
return View(data);
}
Entité Shoe
:
public class ShoeEntity
{
public string Id { get; set; }
public string Name { get; set; }
public ICollection<ShoeImageEntity> ShoeImages { get; set; }
}
Entité 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; }
}
Classe de contexte:
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)
{
}
}
J'ai également essayé la documentation Entity Framework.
Votre ShoeImages déposé correspond à plusieurs champs ImageURL car il est de type ICollection.
Vous pouvez utiliser le code d'affichage suivant pour les afficher.
@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>
Merci à tous pour l'aide. Si je l'avais fait seul, je ne l'aurais pas remarqué. Je vérifie que la réponse est correcte. Je ferai un montage car les deux foreach nécessitent @ avant chaque 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>