You've posted the same code several times. The repository pattern you've come up or copied from the internet does has minor issues. Plus, there's no relationship between Applications and Details.
Below is a basic example using the Hosted Blazor template.
I used a standard service pattern in ASP.NET Core which in my opinion, is more versatile. I do use the repository pattern but for only CRUD operations on a single table. Usually business logic and the UI are far more complex.
public interface IDetailsService
{
Task<List<Details>> GetDetialsByHeaderId(int Id);
}
public class DetailsService : IDetailsService
{
private readonly ApiSqliteContext _context;
public DetailsService(ApiSqliteContext context)
{
_context = context;
}
public async Task<List<Details>> GetDetialsByHeaderId(int Id)
{
var query = _context.Details.Where(x => x.HeaderId == Id);
return await query.ToListAsync();
}
}
The service is registered along with the DbContext
public class ApiSqliteContext : DbContext
{
public ApiSqliteContext(DbContextOptions<ApiSqliteContext> options) : base(options) { }
public DbSet<Details> Details { get; set; }
}
Program.cs
using BlazorWasmHosted.Server.Data;
using BlazorWasmHosted.Server.Services;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddDbContext<ApiSqliteContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("ApiSqliteConnectionString")));
builder.Services.AddScoped<IDetailsService, DetailsService>();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
await BlazorWasmHosted.Server.Models.SeedData.InitializeAsync(services);
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();
Entity Framework Model
public class Details
{
public int ID { get; set; }
public string DetailsName { get; set; }
public int HeaderId { get; set; }
}
DTO (shared between the Web API and Blazor)
public class DetailsDto
{
public int ID { get; set; }
public string DetailsName { get; set; }
public int HeaderId { get; set; }
}
Controller Action
[Route("api/[controller]")]
[ApiController]
public class ApplicationController : ControllerBase
{
private readonly IDetailsService _detailService;
public ApplicationController(IDetailsService detailService)
{
_detailService = detailService;
}
[HttpGet("{id}")]
public async Task<ActionResult<DetailsDto>> Get(int id)
{
List<Data.Details> results = await _detailService.GetDetialsByHeaderId(id);
IEnumerable<DetailsDto> dto = from d in results
select new DetailsDto()
{
ID = d.ID,
DetailsName = d.DetailsName,
HeaderId = d.HeaderId
};
return Ok(dto.ToList());
}
}
Blazor WASM Client
@page "/details"
@using BlazorWasmHosted.Shared
@inject HttpClient Http
<PageTitle>Details</PageTitle>
<h3>Details</h3>
@if (details == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Header Id</th>
</tr>
</thead>
<tbody>
@foreach (var detail in details)
{
<tr>
<td>@detail.ID</td>
<td>@detail.DetailsName</td>
<td>@detail.HeaderId</td>
</tr>
}
</tbody>
</table>
}
@code {
private DetailsDto[]? details;
protected override async Task OnInitializedAsync()
{
details = await Http.GetFromJsonAsync<DetailsDto[]>("/api/Application/1");
}
}
This site has several tutorials that are very good and illustrate basic programming patterns when working with data, Web API, and Blazor.