This is my code from my page:
protected override async Task OnInitializedAsync(){ await Task.Delay(1); Criterias = new ParcoursCriterias() { PageNumber = pageNumber, ElementsByPage = 10 }; ParcoursList = ParcoursService.GetAll(Criterias);}This is the code from App.razor:
@if(RenderModeForPage is not null) {<HeadOutlet @rendermode="@RenderModeForPage" /> } else {<HeadOutlet /> }</head><body> @if(RenderModeForPage is not null) {<Routes @rendermode="@RenderModeForPage" /> } else {<Routes /> }<script src="_framework/blazor.web.js"></script><script src="_content/MudBlazor/MudBlazor.min.js"></script></body></html>@code { [CascadingParameter] private HttpContext HttpContext { get; set; } = default!; private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account") ? null : InteractiveServer;}this is the service function:
public ParcoursDtoList GetAll(ParcoursCriterias criterias){ _logger.LogInformation($"Obtenir plusieurs Parcours: #page {criterias.PageNumber} par l'utilisateur: userId"); // TODO: mettre userID var query = _context.Parcours .AsQueryable() .AsNoTracking(); var comments = Filter(query, criterias); return comments;}private ParcoursDtoList Filter(IQueryable<ParcoursBd> query, ParcoursCriterias criterias){ query = query .OrderByDescending(c => c.Creation); if (criterias.Name != null) { query = query.Where(p => p.Name == criterias.Name); } if (criterias.Description != null) { query = query.Where(p => criterias.Description.Contains(p.Description)); } var totalPages = (int)Math.Ceiling(query.Count() / (decimal)criterias.ElementsByPage); if ((criterias.PageNumber <= 0 || criterias.PageNumber > totalPages) && totalPages > 0) throw new PageNumberNotInRangeException(criterias.PageNumber, totalPages); query = Paginate(query, criterias); return new ParcoursDtoList() { ListParcours = _mapper.Map<List<ParcoursBd>, List<ParcoursDto>>(query.ToList()), PageNumber = criterias.PageNumber, TotalPages = totalPages, ElementsByPage = criterias.ElementsByPage };}private IQueryable<ParcoursBd> Paginate(IQueryable<ParcoursBd> query, ParcoursCriterias criterias){ int startIndex = (criterias.PageNumber - 1) * criterias.ElementsByPage; return query.Skip(startIndex).Take(criterias.ElementsByPage);}When I use "await Task.Delay(1);", I could navigate to my page without being oblige to wait till the loading has finish for the navigation to the page take effect. So when I put the Delay on my page and I click on a button to navigate to it, it navigates to the right page and I can see the loader on it. But, like I said, when I don't put the delay and I click form another page to navigate to my page, the app waits till the loading has finished and then navigate. This is frustrating 😡.
So my question is: am I doing the right thing to Delay on the OnInitializedAsync? What can I do so that it works properly than?
Thank you.