I’m currently working on editing an existing record within a Blazor component. However, I’ve encountered an issue where the updated values are not being saved correctly to the database.After submitting the form and invoking the update method, the changes appear to be overwritten. Upon investigation, I found that the method is reloading the original data from the database immediately after the update, which results in the previous values being restored and the commit not reflecting the intended changes.Could you please advise on a reliable approach to ensure that the updated values are properly saved and not overwritten by the initial data fetch? I’d appreciate any suggestions or best practices to resolve this behaviour?I have given the code below
@code { [Parameter] public int Id { get; set; } public ContentCategory ContentCategory { get; set; } private EditContext editContext; protected override async Task OnInitializedAsync() { if(ContentCategory == null) { ContentCategory = new(); } if (Id > 0 && ContentCategory.Id == 0) // Only load once { var existing = await _contentCategoryRepository.GetByIdAsync(Id); if (existing != null) { ContentCategory = existing; } } //await LoadContentCategory(); } private async Task LoadContentCategory() { if(Id > 0) { ContentCategory = await _contentCategoryRepository.GetByIdAsync(Id); } else { ContentCategory = new ContentCategory(); } editContext = new EditContext(ContentCategory); } private async Task ContentCategoryUpSert(ContentCategory contentCategory) { if(ContentCategory.Id == 0) { await _contentCategoryRepository.CreateAsync(ContentCategory); } else { await _contentCategoryRepository.UpdateAsync(ContentCategory); } _navigationManager.NavigateTo("contentcategories"); }</div><EditForm class="mt-4" Model="ContentCategory" FormName="ContentCategoryAddEdit" OnValidSubmit="() => ContentCategoryUpSert(ContentCategory)"><DataAnnotationsValidator /> <div class="d-flex align-items-center mb-3"><label for="Name" class="form-label me-3 mb-0" style="width: 150px;">Category Name</label><InputText @bind-Value="ContentCategory.Name" class="form-control" id="Name" placeholder="Enter category name" /></div><div class="row mt-3"><div class="col-6 col-md-3"><button type="submit" class="btn btn-primary form-control"><i class="bi bi-floppy2-fill"></i>Update</button></div><div class="col-6 col-md-3"><a href="contentcategories" class="btn btn-secondary form-control"><i class="bi bi-arrow-bar-left"></i>Back to List</a></div></div></EditForm>