Estoy tratando de cargar datos relacionados con Include()
pero creo que estoy haciendo algo mal.
Quiero cargar las imágenes del zapato en esta tabla y las imágenes con fines prácticos en una fila, pero cuando agrego CrateImages.ImageURL
(para dar un ejemplo) aparece un error CS1061 y no puedo mostrar los datos de el Include()
.
Tengo una página de índice:
@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>
Controlador de índice:
public IActionResult Index()
{
var data = _context.Shoes.Include(ci => ci.ShoeImages);
return View(data);
}
Entidad del Shoe
:
public class ShoeEntity
{
public string Id { get; set; }
public string Name { get; set; }
public ICollection<ShoeImageEntity> ShoeImages { get; set; }
}
Entidad de 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; }
}
Clase de contexto:
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)
{
}
}
También probé la documentación de Entity Framework.
Su archivo ShoeImages corresponde a múltiples campos ImageURL porque es del tipo ICollection.
Puede usar el siguiente código de vista para mostrarlos.
@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>
Gracias a todos por la ayuda. Si lo hubiera hecho solo, no me habría dado cuenta. Verifico la respuesta como correcta. Haré una edición porque ambos foreach requieren @ antes de cada 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>