Why in some cases the page will not be refreshed?

Viewed 86

I am building a website with Blazor. On a page I have 6 radiobuttons for choosing a kind of list (Overzichten <=> Previews) and 3 radiobuttons for setting the parameters (... van hitlijsten <=> from charts). When I choose another list, the page is refreshing with a new list on the page. When I choose another chart then nothing will be refreshed.

What I am missing in my code? What do I wrong?

            // In the parent:

        <ul class="list-group lists" role="group">
            <li class="list-group-item subheader">Overzichten...</li>
            <li class="list-group-item lists">
                <input id="nummer1" name="overzichtgroep" type="radio" autocomplete="off" checked @onchange="@SetSelectedView" value="1" /> Alle nummer 1 hits
            </li>
            <li class="list-group-item lists">
                <input id="tipVanDeWeek" name="overzichtgroep" type="radio" autocomplete="off" @onchange="@SetSelectedView" value="2" /> Alle tips van de week
            </li>
...

        <ul class="list-group lists" role="group">
            <li class="list-group-item subheader">... van hitlijsten</li>
            <li class="list-group-item lists">
                <input id="songs" name="hitlijstengroep" type="radio" autocomplete="off" checked @onchange="@SetSelectedView" value="7" /> Songs
            </li>
            <li class="list-group-item lists">
                <input id="albums" name="hitlijstengroep" type="radio" autocomplete="off" @onchange="@SetSelectedView" value="8" /> Albums
            </li>
            <li class="list-group-item lists">
                <input id="nlV" name="hitlijstengroep" type="radio" autocomplete="off" @onchange="@SetSelectedView" value="9" /> Songs NL/V
            </li>
        </ul>

            
                @switch (_previewId)
                {
                    case 1:
                        <div class="row lists">
                            <AllNumber1Hits ListId=@_ListId ListName=@_ListName @ref="_allNumber1Hits" />
                        </div>
                        break;
                    case 2:
                        <div class="row lists">
                            <AllNumber1Tips ListId=@_ListId ListName=@_ListName @ref="_allNumber1Tips" />
                        </div>
                        break;
                    case 3:
        
        .... until case 6.
        
    @code {
        private int _previewId { get; set; } = 1;
        private int _chartId { get; set; } = 7;
        private int _listId { get; set; } = 1;
        private string _listName { get; set; } = "Songs";
        protected override void OnInitialized()
        {
            try
            {
                UpdateView(1);
            }
            catch (AccessTokenNotAvailableException exception)
            {
                exception.Redirect();
            }
        }
                    private void UpdateView(int id)
                    {
                        if (id > 0 && id < 7)
                            _previewId = id;
                        else if (id > 6 && id < 10)
                            _chartId = id;
                
                        switch (_chartId)
                        {
                            case 7:
                                _ListId = 1;
                                _ListName = "Songs";
                                break;
                            case 8:
                                _ListId = 2;
                                _ListName = "Albums";
                                break;
                            case 9:
                                _ListId = 3;
                                _ListName = "Songs NL/V";
                                break;
                        }
                    }
                
                    private void DoRefresh()
                    {
                        if (_allNummer1Hits != null)
                            _allNummer1Hits.Refresh();
        
                        if (_allNummer1Tips != null)
                            _allNummer1Tips.Refresh();
                    }
                
                
                    public void SetSelectedView(ChangeEventArgs args)
                    {
                        if (args != null && args.Value != null)
                        {
                            int id;
                            int.TryParse(args.Value.ToString(), out id);
                
                            UpdateView(id);
                            DoRefresh();
                        }
                    }
            
                
               // In the child:
            
               @code
                {
                    private List<AllNumber1HitsModel> model { get; set; }
                    [Parameter] public int ListId { get; set; }
                    [Parameter] public string ListName { get; set; } = string.Empty;
                
                    protected override async Task OnInitializedAsync()
                    {
                        try
                        {
                            model = await _statisticsService.GetAllNumber1Hits(ListId, ListName);
                        }
                        catch (AccessTokenNotAvailableException exception)
                        {
                            exception.Redirect();
                        }
                    }
                
                    public void Refresh() 
                    { 
                        StateHasChanged(); 
                    }
                }
1 Answers

I've have change my code by using an event OnParameterSet. ... and then I use the same code as OnInitialize.

This works fine:

protected override async Task OnParametersSetAsync()
{
    await UpdateList();
}

protected override async Task OnInitializedAsync()
{
    try
    {
        await UpdateList();
    }
    catch (AccessTokenNotAvailableException exception)
    {
        exception.Redirect();
    }
}

private async Task UpdateList()
{
    model = await _allStatisticsService.GetAllNumber1Hits(ListId, ListName);
}
Related