Ich versuche, verwandte Daten mit Include()
zu laden, aber ich glaube, ich mache etwas falsch.
Ich möchte die Bilder des Schuhs in diese Tabelle und die Bilder zu Übungszwecken in einer Zeile CrateImages.ImageURL
, aber wenn ich CrateImages.ImageURL
hinzufüge (um ein Beispiel zu geben), erhalte ich eine Fehlermeldung CS1061 und kann die Daten von nicht anzeigen das Include()
.
Ich habe eine Indexseite:
@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>
Indexregler:
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-Entität:
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; }
}
Kontextklasse:
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)
{
}
}
Außerdem habe ich die Entity Framework-Dokumentation ausprobiert.
Ihre ShoeImages-Datei entspricht mehreren ImageURL-Feldern, da sie vom Typ ICollection ist.
Sie können den folgenden Ansichtscode verwenden, um sie anzuzeigen.
@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>
Vielen Dank an alle für die Hilfe. Wenn ich es alleine gemacht hätte, hätte ich es nicht bemerkt. Ich überprüfe die Antwort als richtig. Ich werde eine Bearbeitung vornehmen, da für beide foreach vor jedem foreach @ erforderlich ist
@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>