为什么在某些情况下页面不会被刷新



我正在与Blazor建立一个网站。在一个页面上,我有6个用于选择列表类型的单选按钮(Overzichten<=>预览(和3个用于设置参数的单选按钮。当我选择另一个列表时,页面将刷新为页面上的新列表。当我选择另一个图表时,将不会刷新任何内容。

我的代码中缺少什么?我做错了什么?

// 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(); 
}
}

我已经使用事件OnParameterSet更改了代码。…然后我使用与OnInitialize相同的代码。

这很好:

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);
}

相关内容

  • 没有找到相关文章