I'm new to blazor (server) and I'm building a small application. I have a parent component "AccountCard.razor" a child component called "ContactsEditorModal.razor" and a child component called "ContactsList.razor". Both use their own models.
In the ContactsEditorModal I can add a contactperson. And with the EventCallback method I can do some update stuff. What I have working is that I can update the list in the ContactsList.razor after I've added a contact. But the parent component is not updated. Note, in my test, I first call the parent. When everything is rendered and on screen, I change the name of the Account in the database via SQL. Then I add the new contact (via the modal). I can see that the HandleContactModal method is hit (it updates the child component "ContactList". I also notice that the UpdateData method is called, but the result is the old object and not the object with the adjusted name.
AccountCard.razor
@page "/app/accounts/card/{id}"
<h3>Component1</h3>
<div>@Id</div>
<div>@Account.Name</div>
<button class="btn btn-primary" @onclick="() => modal.Open()">Contactperson: New</button>
<ContactList @ref="contactList" AccountId="@Account.AccountId" />
<ContactEditorModal @ref="modal" AccountId="@Account.AccountId.ToString()" CloseEventCallback="HandleContactModal">
<Body>
test
</Body>
</ContactEditorModal>
@code {
[Inject]
private IAccountRepository accountRepository { get; set; }
public Entities.Account Account { get; set; } = new Entities.Account();
[Parameter]
public string Id { get; set; } = "";
private ContactEditorModal modal { get; set; } = new ContactEditorModal();
private ContactList contactList { get; set; } = new ContactList();
protected override async Task OnInitializedAsync()
{
await UpdateData();
}
private async Task UpdateData()
{
try
{
Account = accountRepository.GetAccounts().Result.FirstOrDefault(p => p.AccountId == Guid.Parse(Id));
}
catch(Exception ex)
{
throw;
}
}
async Task HandleContactModal(bool test)
{
await UpdateData();
await contactList.UpdateData();
StateHasChanged();
}
public string GetAddUrl() => $"/app/contacts/add/{Id}";
}
The contactList.Update() is a function in the ContactList.razor component. This function is executed and the data is updated.
ContactList.razor (update function)
public async Task UpdateData()
{
if (AccountId == Guid.Empty)
{
Contacts = ContactRepository.GetContacts().ToList();
}
else
{
Contacts = ContactRepository.GetContacts().Where(p => p.Accounts.Any(p => p.AccountId == AccountId)).ToList();
StateHasChanged();
}
}
The DbContext looks like this
DataContext.cs public class DataContext : DbContext { public DataContext(DbContextOptions opts) : base(opts) { }
public DbSet<Account> Accounts => Set<Account>();
public DbSet<Contact> Contacts => Set<Contact>();
}
And I'm using it in my AccountRepository (and also ContactRepository) like this:
AccountRepository.cs
public class AccountRepository : IAccountRepository
{
private readonly DataContext _ctx;
public AccountRepository(DataContext ctx)
{
_ctx = ctx;
}
public async Task<IQueryable<Account>> GetAccounts()
{
return _ctx.Accounts;
}
public void CreateAccount(Account account)
{
_ctx.Add(account);
_ctx.SaveChanges();
}
public void SaveAccount(Account account)
{
_ctx.SaveChanges();
}
}