My problem may be simple but I have been going round and round trying to understand what the problem is. I have a Blazor page with separate model class (references as LsC for the List<Type>
or ls for single when looped in code) that loads the data. But when I click to update that data the page does not refresh. If I add a new entry, that page does load the new entry.
The Blazor page code with the problem is simple:
@if (ls.PrimaryContact)
{
<div style="cursor: pointer;" class="badge badge-pill badge-success">Primary</div>
}
else
{
<div style="cursor: pointer;" @onclick="@(() =>MakePrimaryContact(@ls.Id))" class="badge badge-pill badge-info">Make Primary</div>
}
The Code should put a green pill when the result in the primary field is true, or put a regular info pill with the make primary text. The code works correctly when the page is manually (F5) refreshed. But when I click on the Make Primary pill, the database does update, but the page does not. I have to again manually update the page to properly reflect the change.
The function it calls onClick:
private void MakePrimaryContact(int LCId)
{
LeadsContactsGateway LCGw = new LeadsContactsGateway();
var wt = LCGw.UpdatePrimary(Lead.Id, LCId);
LsC = LsCGw.GetByLeadsId(Id);
StateHasChanged();
OnInitialized();
}
I added the StateHasChanged()
as an additional measure, but yet my page does not fetch the updated recordset with the correct primary contact. It is not until I manually (F5) refresh the web page that the proper update is reflected.
Strangely enough on this same page, I have yet another button that adds a new database entry and that update pops in automatically.
Code for new database entry that does NOT have a problem:
<button class="btn btn-primary btn-sm" @onclick="() => NewLeadContactModal.Open()">+ New
<i class="icon icon-people-fill icon-white"></i>
</button>
...
<Modal @ref="@NewLeadContactModal">
<Title>Add New Contact for: @Lead.LeadName</Title>
<Body>
<ContactAdd LeadsId="@Lead.Id"></ContactAdd>
</Body>
<Footer>
<button type="button" class="btn btn-secondary" data-dismiss="modal" @onclick="() => ContactModalClickHandler(0)">Close</button>
</Footer>
...
private void ContactModalClickHandler(int completed)
{
if (completed > 0)
{ }
NewLeadContactModal.Close();
OnInitialized();
}
Any suggestions would be appreciated.
ls
orLead
– Peter MorrisOnInitialized
does some work you want to re-use elsewhere, move it to a separate method and call that method from both. @peter 's advice is good – Quango