I have a blazor server application with a service base backend (no API).
I’m not posting the real code due to company policies, so I provided the following example.
I have a button that when clicked triggers a method that searchs for an element with an id.
Index.razor
<MudNumericField HideSpinButtons="true"
@bind-Value="ElementNumber"
Label="Element number"
Variant="Variant.Text"
Required
id="element-number-id"
OnKeyUp="OnEnterPressed"
RequiredError="Cannot be empty"
Placeholder="Type a number" />
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
id="element-search-btn"
OnClick="OnSearchElement"
Disabled="@_loading_btn">Search</Mudbutton>
@code {
public IEnumerable<ElemDTO>? Elems { get; set; }
public IEnumerable<ElemDTO>? CopyElems { get; set; }
public IEnumerable<ElemDTO>? TmpElems { get; set; }
public int? ElementNumber {get;set;} = default!;
public async void OnSearchElement() {
TmpElems = await _service.getElementByNumber(ElementNumber)
Elems = CopyElems = FormatElement(TmpElems);
await InvokeAsync(() => this.StateHasChanged());
// Elems is used for rendering the collection on a table.
}
public IEnumerable<ElemDTO>? FormatElement(IEnumerable<ElemDTO>? elems) {
// this method modifies the elems collection
elems.FirstOrDefault().Prop1 = false;
// it overrides some of their properties.
return elems;
}
}
This example above is working just fine for the first time I click on the button. The issue appears when I click again the Mudbutton to search. It triggers the service call as expected (I debugged step by step and it is going across all the expected paths) but the weird thing is that the collection remains in a modified state, like the one shown in the example: Prop1
remains false
and the original value (the one from the database) is true
. Is this caching the search results from the previous query in some way? Any ideas??